2017-10-04 72 views
-3

背景如何导入扬鞭配置

我遇到了一些扬鞭配置的问题,所以我想通过复制一些简单的例子的CONFIGS解决这些问题。

我阅读本教程: http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

而且他们有这样的:

@Configuration 
@EnableSwagger2 
public class SwaggerConfig {          
    @Bean 
    public Docket api() { 
     return new Docket(DocumentationType.SWAGGER_2) 
      .select()         
      .apis(RequestHandlerSelectors.any())    
      .paths(PathSelectors.any())       
      .build();           
    } 
} 

设置

我曾经有过这个bean案在我public class Application但他们似乎有在它自己的类中的配置。我想匹配它们的设置,所以我在与Application.java相同的位置创建了一个SwaggerConfiguration.java文件。

然后我做了SwaggerConfiguration.java包含以下代码:

@Configuration 
@EnableSwagger2 
public class SwaggerConfiguration { 

    @Bean 
    public Docket Api() { 
     return new Docket(DocumentationType.SWAGGER_2) 
       .select() 
       .apis(RequestHandlerSelectors.any()) 
       .paths(PathSelectors.any()) 
       .build() 
       .pathMapping("/") 
       .apiInfo(apiInfo()); 
    } 


    private ApiInfo apiInfo() { 
     return new ApiInfoBuilder() 
      .title("Service API") 
      .build(); 
    } 
} 

我Application.Java包含此代码:

@SpringBootApplication 
@EnableTransactionManagement 
@EnableSwagger2 
@ComponentScan({"myproject.request"}) 
public class Application { 

    public static void main(String[] args) { 
     new SpringApplication(Application.class).run(args); 
    } 

} 

问题:如何配合这个SwaggerConfiguration.java进入我的项目? (导入)

他们这样做是在这里,我相信:“进口豆在现有的休息 - 调度 - servlet.xml中的组件扫描标签添加软件包的名称(如果缺少)” - http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

但是,我没有那个serverlet,也没有任何xml文件,除了maven的pom.xml。

问题转述

我SwaggerConfig.java只是坐在我的项目,但不使用或进口被任何东西。例如apiInfo未在Swagger UI上设置任何内容。我如何使用它。

更新1

有一个建议,即删除@ComponentScan({"myproject.request"})但是当我做了我的构建失败,这是印:

Description: 

Field actionRepository in myproject.service.ActionServiceImpl required a bean of type 'myproject.repository.ActionRepository' that could not be found. 


Action: 

Consider defining a bean of type 'myproject.repository.ActionRepository' in your configuration. 

更新2

我已经改变SwaggerConfiguration SwaggerConfig

@Configuration 
@EnableSwagger2 
public class SwaggerConfig { 

    /* 
    @Bean 
    public Docket Api() { 
     return new Docket(DocumentationType.SWAGGER_2).select() 
       .apis(RequestHandlerSelectors.basePackage("myproject.controller")).paths(regex("/api/*")) 
       .build().apiInfo(apiInfo()); 

    } 
    */ 
    @Bean 
    public Docket Api() { 
     return new Docket(DocumentationType.SWAGGER_2) 
       .select() 
       .apis(RequestHandlerSelectors.basePackage("ibm.controller")) 
       .paths(regex("/api/*")) 
       .build() 
       .pathMapping("/") 
       .apiInfo(apiInfo()); 
    } 


    private ApiInfo apiInfo() { 
     return new ApiInfoBuilder() 
      .title("Service API") 
      .description("API for Service REST operations") 
    } 
} 

但我也有同样的问题

UPDATE 3个背景

这个问题在一定程度上依赖于这个问题Swagger no longer finds API controllers

+0

取出后再次尝试'@ComponentScan({ “myproject.request”})'中的应用。注释'@ SpringBootApplication'应该处理所有这些。 –

+0

@LucianovanderVeekens看到我的更新1 – Rorschach

回答

1
package com.vk.test.swagger; 

import static springfox.documentation.builders.PathSelectors.regex; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 

import springfox.documentation.builders.RequestHandlerSelectors; 
import springfox.documentation.spi.DocumentationType; 
import springfox.documentation.spring.web.plugins.Docket; 
import springfox.documentation.swagger2.annotations.EnableSwagger2; 

@Configuration 
@EnableSwagger2 

/** 
* 
* @author vaquar khan 
* 
*/ 
public class SwaggerConfiguration { 
    @Bean 
    public Docket productApi() { 
     return new Docket(DocumentationType.SWAGGER_2).select() 
       .apis(RequestHandlerSelectors.basePackage("com.vk.test.controller")).paths(regex("/api/apiPath.*")) 
       .build(); 

    } 

} 

Maven的

 <!-- Swagger --> 
     <dependency> 
      <groupId>io.springfox</groupId> 
      <artifactId>springfox-swagger2</artifactId> 
      <version>2.6.1</version> 
      <scope>compile</scope> 
     </dependency> 
     <!-- Swagger UI --> 

     <dependency> 
      <groupId>io.springfox</groupId> 
      <artifactId>springfox-swagger-ui</artifactId> 
      <version>2.6.1</version> 
      <scope>compile</scope> 
     </dependency> 

呼叫招摇

http://<servername>:<Port>/swagger-ui.html 
+0

除了将'SwaggerConfiguration'重命名为'SwaggerConfig'并在'PathSelectors'上使用正则表达式之外,我无法分辨您与我做了什么不同。我错过了什么? (“/ api/api Path。*”)作为(“myproject /”)。 – Rorschach

+0

控制器“)路径或字面意思(”/ api/api路径。*“) – Rorschach

+0

不应该它是应用程序上的api URI - @RestController @RequestMapping(value =”/ api/api Path“) @Api value =“onlinestore”,description =“dgh kjgkj kbkjh”) –