2017-04-05 98 views
0

我試圖通過生成招搖 無彈簧引導 一些API,但API文檔不起作用創建招搖

@Configuration 
@EnableSwagger2 
@Controller 
@RequestMapping("/srs/api") 
public class SwaggerConfig extends WebMvcConfigurerAdapter { 
    @Bean 
    @RequestMapping(value = "/v2/api-docs", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
    public Docket swaggerconf() { 
     return new Docket(DocumentationType.SWAGGER_2) 
       .useDefaultResponseMessages(false) 
       .apiInfo(apiInfo("2.0")) 
       .select() 
       .apis(RequestHandlerSelectors.any()) 
       .paths(PathSelectors.any()) 
       .build(); 
    } 
    private ApiInfo apiInfo(String version) { 
     return new ApiInfoBuilder() 
       .title("API") 
       .description("REST API") 
       .version(version) 
       .build(); 
    } 
} 

URL映射我昂首闊步控制器類

Mapped "{[/srs/api/v2/api-docs],methods=[GET],produces=[application/json]}" onto public springfox.documentation.spring.web.plugins.Docket com.my.applications.srs.rest.controllers.SwaggerConfig.swaggerconf() 

但是文檔沒有創建 我錯過了什麼嗎? 可能是我可以在服務器上使用SpringBoot?

+0

你太依賴於'springfox-招搖,ui'依賴? –

+0

@NicolasLabrot肯定 我添加了所有必要的依賴項 –

+0

你的日誌是否包含'Mapped「{[/ swagger-resources/configuration/ui]}」'? –

回答

1

你能嘗試更新您的招搖配置:

@Configuration 
@EnableSwagger2 
public class SwaggerConfig { 
    @Bean 
    public Docket swaggerconf() { 
     return new Docket(DocumentationType.SWAGGER_2) 
       .useDefaultResponseMessages(false) 
       .apiInfo(apiInfo("2.0")) 
       .select() 
       .apis(RequestHandlerSelectors.any()) 
       .paths(PathSelectors.any()) 
       .build(); 
    } 
    private ApiInfo apiInfo(String version) { 
     return new ApiInfoBuilder() 
       .title("API") 
       .description("REST API") 
       .version(version) 
       .build(); 
    } 
} 

然後導入(如果尚未完成)@Import這個配置到你的Application像類。

當你引導你的應用程序,你應該有一個日誌像

Mapped "{[/v2/api-docs],methods=[GET],produces=[application/json || application/hal+json]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest) 

如果你打開URL /v2/api-docs你應該得到一個JSON

+0

謝謝你的建議 我跟你拼命地跑的配置,但在日誌中我無法找到映射/ V2/API - 文檔:( –

+0

你能設置'返回新案(DocumentationType.SWAGGER_2)斷點'線和調試運行你的應用程序嗎?您的調試器停止了嗎? –

+0

沒有,調試器不會停止在這裏 –