2017-10-04 62 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」) –