1

I'm writing some API and I want to integrate Documentation while writing code, so I found Swagger as a good way to to this.

I used Swagger core v3 notations, so my classes are something like:

@RestController

@RequestMapping("/api/v1/bundles")

@OpenAPIDefinition(

        info = @Info(
                title = "Lifecycle Management RESTful API.",
                version = "1",
                description = "TODO",
                license = @License(name = "Apache 2.0", url = "xxx"),
                contact = @Contact(url = "xxx", name = "xx", email = "[email protected]")
        ))

public class RestBundle {

    @GetMapping(value = "/{nodeId}",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    @Operation(summary = "Get all bundles",
            description = "Get all available bundles status from a specific node")
    public Something(..)  {
        //Something ...
    }

}

And I create a configuration class:

@Configuration
@EnableSwagger2

public class SwaggerConfig {


    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }


    ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("ANF Orchestrator")
                .description("REST API for ANF Orchestrator")
                .license("Apache 2.0")
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .termsOfServiceUrl("")
                .version("1.0.0")
                .contact(new Contact("Amine","xxx", "[email protected]"))
                .build();
    }
}

ANd I want to enable the UI of Swagger to get the documentation, but when I enter to: .../swagger-ui.html

I get:

Unable to render this definition The provided definition does not specify a valid version field.

Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n(for example, openapi: 3.0.0).

2 Answers 2

1

Try to extend your SwaggerConfig class with WebMvcConfigurationSupport and override its method called addResourceHandlers with implementation like this:

@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("swagger-ui.html")
            .addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this worked, but I get an error exception: protected void addResourceHandlers(ResourceHandlerRegistry registry) {
0

On top of class, can you try removing this annotation, as the SwaggerConfig already contains the Docket info.

  @OpenAPIDefinition(

            info = @Info(
                    title = "Lifecycle Management RESTful API.",
                    version = "1",
                    description = "TODO",
                    license = @License(name = "Apache 2.0", url = "xxx"),
                    contact = @Contact(url = "xxx", name = "xx", email = "[email protected]")
            ))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.