Springfox swagger - 没有带有 spring boot jersey 和 gradle 的 api-docs [英] Springfox swagger - no api-docs with spring boot jersey and gradle

查看:41
本文介绍了Springfox swagger - 没有带有 spring boot jersey 和 gradle 的 api-docs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 jersey 和 gradle 的 spring boot 应用程序,我正在尝试使用 springfox 自动生成 API 文档.

I have a spring boot application with jersey and gradle, and I am trying to automatically generate the API documentation using springfox.

我已按照此处的步骤操作:http://springfox.github.io/springfox/文档/当前/

I have followed the steps here: http://springfox.github.io/springfox/docs/current/

这是我所做的:

  • build.gradle:

  • build.gradle:

dependencies {
    .........
    //Swagger
    compile "io.springfox:springfox-swagger2:2.4.0"
    compile "io.springfox:springfox-bean-validators:2.4.0"
    compile 'io.springfox:springfox-swagger-ui:2.4.0'
}

  • Spring boot 应用:

  • Spring boot Application:

    @SpringBootApplication
    @EnableSwagger2
    public class AnalyzerServiceApplication{
    
    public static void main(String[] args) {
        SpringApplication.run(AnalyzerServiceApplication.class, args);
    }
    
    @Bean
    public Docket analyzerApi() {
    return new Docket(DocumentationType.SWAGGER_2)
    .select()
        .apis(RequestHandlerSelectors.any())
        .paths(PathSelectors.any())
        .build()
    .pathMapping("/")
    .directModelSubstitute(LocalDate.class, String.class)
    .genericModelSubstitutes(ResponseEntity.class)
    .alternateTypeRules(
        newRule(typeResolver.resolve(DeferredResult.class,
        typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
        typeResolver.resolve(WildcardType.class)))
    .useDefaultResponseMessages(false)
    .globalResponseMessage(RequestMethod.GET,
        newArrayList(new ResponseMessageBuilder()
            .code(500)
            .message("500 message")
            .responseModel(new ModelRef("Error"))
            .build()))
    .securitySchemes(newArrayList(apiKey()))
    .securityContexts(newArrayList(securityContext()))
    .enableUrlTemplating(true)
    .globalOperationParameters(
        newArrayList(new ParameterBuilder()
            .name("someGlobalParameter")
            .description("Description of someGlobalParameter")
            .modelRef(new ModelRef("string"))
            .parameterType("query")
            .required(true)
            .build()))
        .tags(new Tag("Pet Service", "All apis relating to pets")) 
        ;
    }
    
    @Autowired
    private TypeResolver typeResolver;
    
    private ApiKey apiKey() {
        return new ApiKey("mykey", "api_key", "header");
    }
    
    private SecurityContext securityContext() {
        return SecurityContext.builder()
            .securityReferences(defaultAuth())
            .forPaths(PathSelectors.regex("/anyPath.*"))
            .build();
    }
    
    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope
            = new AuthorizationScope("global", "accessEverything");
            AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return newArrayList(
            new SecurityReference("mykey", authorizationScopes));
    }
    
    @Bean
    SecurityConfiguration security() {
        return new SecurityConfiguration(
            "test-app-client-id",
            "test-app-client-secret",
            "test-app-realm",
            "test-app",
            "apiKey",
            ApiKeyVehicle.HEADER, 
            "api_key", 
            "," /*scope separator*/);
    }
    
    @Bean
    UiConfiguration uiConfig() {
        return new UiConfiguration("validatorUrl");
    }
    

  • 现在是控制器(泽西)

  • Now the controller (Jersey)

    @Api(value = "/widget")
    @Path("/widget")
    @Component
    public class WidgetController extends BaseController {
    
    @Autowired
    private WidgetService widgetService;
    
    @GET
    @Path("/secHealth")
    @ApiOperation(value = "Find pet by ID", notes = "Returns a pet when ID < 10.  ID > 10 or nonintegers will simulate API error conditions", response = Pet.class)
    @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid ID supplied"),
    @ApiResponse(code = 404, message = "Pet not found") })
    public Response getPet() {
        //Do something
    }
    

  • 当我启动服务器并导航到 http://localhost:8080/swagger-ui.html,我可以看到绿色"UI 屏幕,其中仅列出了基本错误控制器.我自己的控制器不在那里.

    When I start the server and navigate to http://localhost:8080/swagger-ui.html, I can see the "green" UI screen with only the basic-error-controller listed there. My own controller is not there.

    我做错了什么?谢谢伙计

    What did I do wrong? Thanks Guy

    推荐答案

    截至版本 2.5.0 springfox 仅支持 spring-mvc 控制器.不支持像 jersey 这样的 Jax-rs 实现.

    As of version 2.5.0 springfox only supports spring-mvc controllers. Jax-rs implementations like jersey aren't supported.

    当前使用 springfox 的替代方法是使用 swagger-core 库进行 jax-rs/jersey 基于服务.

    The current alternative to using springfox is to use the swagger-core library for jax-rs/jersey based services.

    它确实具有在 2.6+ 中实现对球衣的支持所需的钩子.这是 this issue

    It does have the hooks needed to implement support for jersey in 2.6+. Here is an excerpt of a way to implement it in this issue

    目前 ResourceConfig 有一个名为getClasses"的方法,它将列出所有注册的内容.像资源,过滤器等......也许这个有帮助.但请注意,返回的课程也可能是过滤器或任何其他您可以在 jersey2 注册的东西.

    Currently ResourceConfig has a method called "getClasses" which will list everything registerted. like Resources, Filters,etc... Maybe this could help. But be aware that the returning classes could also be filters or any other stuff you could register with jersey2.

    这篇关于Springfox swagger - 没有带有 spring boot jersey 和 gradle 的 api-docs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆