Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Springfox swagger - no api-docs with spring boot jersey and gradle

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

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

Here is what I did:

  • 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 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
    }
    

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

like image 627
Guy Hudara Avatar asked Jun 05 '16 10:06

Guy Hudara


People also ask

Does Springfox support OpenAPI 3?

Recently, the popular Springfox project released the long-awaited v3 of their library with support for OpenAPI 3 and Spring 5 (only annotation-based API is supported).

How do I enable Swagger in spring boot?

To enable the Swagger2 in Spring Boot application, you need to add the following dependencies in our build configurations file. For Gradle users, add the following dependencies in your build. gradle file. Now, add the @EnableSwagger2 annotation in your main Spring Boot application.

What is Springfox Swagger?

Springfox is a set of Java libraries, that has evolved from the swagger-springmvc project. It automates the generation of specifications for JSON APIs, implemented with the Spring framework. Also, it provides libraries to integrate the Swagger UI to interact with APIs.


1 Answers

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

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

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

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.

like image 146
Dilip Krishnan Avatar answered Sep 23 '22 21:09

Dilip Krishnan