Heisenberg1
Heisenberg1

Reputation: 51

Implementation of Swagger core v3 in java

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).

Upvotes: 1

Views: 4926

Answers (2)

TechFree
TechFree

Reputation: 2974

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]")
            ))

Upvotes: 0

Meshpy
Meshpy

Reputation: 102

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/");
}

Upvotes: 1

Related Questions