TKPhillyBurb
TKPhillyBurb

Reputation: 171

Swagger REST API documentation with Spring Boot

I want to use Swagger 2.0 with my Spring Boot RESTful web service to generate documentation. I have searched quite a bit for an answer to this. Basically I have a Spring Boot project with a set of controllers and I want to document the API's. I have the following dependencies setup in my POM file.

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.4.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.5.0</version>
    </dependency>

This is my Swagger configuration class with the @Configuration and @EnableSwagger2:

      @Configuration
      @EnableSwagger2
public class SwaggerConfig {

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

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("My application title")
            .description("This is a test of documenting EST API's")
            .version("V1.2")
            .termsOfServiceUrl("http://terms-of-services.url")
            .license("LICENSE")
            .licenseUrl("http://url-to-license.com")
            .build();
    }

}

From what I have gathered in reading a couple of other answers here that at this point I should be able to see something at a URL such as http://myapp/v2/api-docs or alternatively http://localhost:8080/myapp/api-docs I have made the assumption that the "myapp" portion of the above URL refers to the name of the class in which my main resides (is this correct)? Also I have tried this with port 8080 and port 80 and the bottom line is that I see nothing other than site can't be reached. I have looked at the answers provided here and here however I'm not having any success. Any help would be much appreciated, thank you in advance.

Upvotes: 2

Views: 4962

Answers (4)

hardMaster
hardMaster

Reputation: 21

Since the required dependencies are already being used in the pom.xml file, here is the implementation that can be used for Swagger 2 in the spring boot project for all the rest-controller classes.

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig implements WebMvcConfigurer {

    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry
                .addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry
                .addResourceHandler("/webjars/**")
                .addResourceLocations("classpath://META-INF/resources/webjars/");
    }

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    UiConfiguration uiConfiguration() {
        return UiConfigurationBuilder.builder()
                .defaultModelExpandDepth(-1)
                .build();
    }
}

The following config file can be modified and used as per needs especially in the uiConfiguration() method. The rest-controller wherever is needed can be annotated with @Api on the class and @ApiOperation on the method of interest such as either a post/get call. Feel free to explore.

Upvotes: 1

Rupendra Sharma
Rupendra Sharma

Reputation: 250

1- Use this url to access swagger documentation- 
   http://localhost:8080/swagger-ui/index.html
   http://localhost:8080/swagger-ui.html
2- To view OpenAPI description-
   http://localhost:8080/v3/api-docs

Here you can find integration example with sample source code-
https://javadrill.com/spring-boot-3-swagger-integration-example

Upvotes: 0

Gaganam Krishna
Gaganam Krishna

Reputation: 151

I used, <artifactId>springdoc-openapi-ui</artifactId> with

public class OpenApiConfiguration{

     @Bean
     public GroupedOpenApi abcApp(){
        String[] abcAppRootPath={"com.stockoverflow.swagger"};
        return GroupedOpenApi.builder().group("my app").packagesToScan(abcAppRootPath).build();
    }
}

reference : https://springdoc.org/#getting-started

Upvotes: 0

fbak
fbak

Reputation: 21

As you can see on the following documentation : https://springfox.github.io/springfox/docs/snapshot/#springfox-swagger-ui

The endpoint is now on swagger-ui.html, for your case, it will be http://localhost:8080/myapp/swagger-ui.html

Upvotes: 2

Related Questions