3
votes

I have a REST application using Spring Boot 2.2.4.RELEASE. My REST controller is annotated like

@RestController
@RequestMapping("/")

My REST controller has @GetMapping, @PostMapping, etc., it works as expected.

Now I want to integrate Swagger in latest version.
https://github.com/swagger-api/swagger-core
https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Getting-started

The Maven dependencies shown there I added to my pom.xml:

<dependency>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-core</artifactId>
    <version>2.1.1</version>
</dependency>
<dependency>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-annotations</artifactId>
    <version>2.1.1</version>
</dependency>
<dependency>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-models</artifactId>
    <version>2.1.1</version>
</dependency>
<dependency>
    <groupId>io.swagger.core.v3</groupId>
    <artifactId>swagger-integration</artifactId>
    <version>2.1.1</version>
</dependency>

According to the 'Getting Started', just by adding the dependencies I should see the generated OpenAPI at http://localhost:8080/openapi.json but nothing shows up there.

Is Swagger (swagger-core,... in version 2.1.1) usable with Spring Boot web applications?

There is a project SpringFox, but it is not up to date. Also springdoc-openapi is available. But using Swagger directly would be my first thought.

1
Yes, you can use swagger directly in Spring boot. Have you configured Swagger in your project yet? - Rohan Shah
No, what kind of configuration is needed? I just used one @Operation(summary="...", description="...") annotation above one of the @GetMapping methods. Are more dependencies needed? - neblaz
I have added a Basic Config file in the answer below. - Rohan Shah

1 Answers

0
votes

You'll need a SwaggerConfig file in your code.

Configuration needed in Swagger for Java goes like this: (Note that this is just the basic File and you can Configure it however you want.)

@EnableSwagger2
@Configuration
public class SwaggerConfig {

    public static String bucketName;

    @Value("${swagger.config.basePackage}")
    public void setName(String name) {
        bucketName = name;
    }

    @Bean
    public Docket classifiedApi()
    {
        ArrayList<ApiKey> apiKeys=new ArrayList<>();
        apiKeys.add(apiKey());
        return new Docket(DocumentationType.SWAGGER_2).securitySchemes(apiKeys)
                .select()
                .apis(RequestHandlerSelectors.basePackage(bucketName))
                .build()
                .apiInfo(metaData());
    }

    private ApiKey apiKey() {
        return new ApiKey("Api Key", Constants.JWTToken.API_KEY, "header");
    }

    private ApiInfo metaData() {
        return new ApiInfoBuilder()
                .title("APPNAME REST API")
                .description("Spring Boot REST API for APPNAME")
                .contact(new Contact("YOURNAME ", "Coming Soon ...", "CONTACT ADDRESS"))
                .build();
    }
}