gradle - Springfox swagger - 没有带有 spring boot jersey 和 gradle 的 api-docs

标签 gradle spring-boot jersey swagger springfox

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

我已按照此处的步骤操作:http://springfox.github.io/springfox/docs/current/

这是我所做的:

  • 构建.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 启动应用程序:
    @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");
    }
    
  • 现在的 Controller (泽西)
    @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 屏幕,其中仅列出了基本错误 Controller 。我自己的 Controller 不在那里。

    我做错什么了?
    谢谢
    盖伊

    最佳答案

    从版本 2.5.0 开始springfox仅支持 spring-mvc Controller 。不支持像 jersey 这样的 Jax-rs 实现。

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

    它确实具有在 2.6+ 中实现对 Jersey 的支持所需的钩子(Hook)。 .这是 this issue 中实现它的方法的摘录

    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.

    关于gradle - Springfox swagger - 没有带有 spring boot jersey 和 gradle 的 api-docs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37640863/

    相关文章:

    intellij-idea - Intellij IDEA-无法部署到tomcat

    c++ - Gradle cpp 插件不链接共享库

    spring - 如何允许匿名用户仅使用 spring security 访问某个功能

    java - Spring Boot 1.5.8 中的内存泄漏

    java - Eclipse 插件开发 - Gradle 无法从 Eclipse 启动器运行 : java. io.IOException:访问被拒绝

    spring-boot - 从休息端点停止调度作业

    java - jax-rs 的多个 PathParam

    java - 无法访问 MS Project Server 2013 REST API 服务器 - 授权错误

    java - Jersey 不遵循 302 重定向

    java - 如何在Gradle中禁止Spotbugs stacktrace?