大摇大摆地创建 api 文档

create api doc with swagger

我尝试通过 swagger 生成一些 api 没有 spring 引导 但它不起作用

我的招摇控制器class

@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?

您能否尝试将您的 swagger 配置更新为:

@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 中,例如 class。

当您 bootstrap 您的应用程序时,您应该有一个像

这样的日志
 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