小编典典

Springfox swagger-没有带有Spring Boot球衣和Gradle的API文档

spring-boot

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

我在这里遵循了以下步骤:http :
//springfox.github.io/springfox/docs/current/

这是我所做的:

  • 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应用程序:
        @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");
    }
  • 现在是控制器(泽西岛)
        @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时
,我可以看到仅在其中列出了basic-error-
controller的“绿色” UI屏幕。我自己的控制器不在那里。

我做错什么了?哥们,谢啦


阅读 400

收藏
2020-05-30

共1个答案

小编典典

从版本开始,2.5.0 springfox仅支持spring-mvc控制器。不支持jersey等Jax-
rs实现。

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

它确实具有在中实现对球衣的支持所需的挂钩2.6+。这是本期实施方法的摘录

当前,ResourceConfig具有一个名为“
getClasses”的方法,该方法将列出所有已注册的内容。例如资源,过滤器等…也许可以帮上忙。但是请注意,返回的类也可以是过滤器或可以在jersey2中注册的任何其他内容。

2020-05-30