Configuring Swagger UI with Spring Boot

后端 未结 4 1149
生来不讨喜
生来不讨喜 2020-12-17 00:15

I am trying to configure Swagger UI with my Spring boot application. Although the v2/api-docs seems to be loading properly, the http://localhost:8080/swag

相关标签:
4条回答
  • 2020-12-17 00:52

    I had this issue today and fixed it by matching up the versions of my springfox-swagger2 and springfox-swagger-ui dependencies:

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

    There's very little other code to just get it up and running. One simple config class:

    @Configuration
    @EnableSwagger2
    class SwaggerConfiguration {
    
        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.foo.samples.swaggersample"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
    }
    

    And my application.properties

    # location of the swagger json
    springfox.documentation.swagger.v2.path=/swagger.json
    

    (This is in Spring Boot).

    0 讨论(0)
  • 2020-12-17 00:56

    Add a config class like this

    @Configuration
    public class WebMvcConfiguration extends WebMvcConfigurationSupport {
    
      @Override
      public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        // Make Swagger meta-data available via <baseURL>/v2/api-docs/
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        // Make Swagger UI available via <baseURL>/swagger-ui.html
        registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/");
      }
    }
    
    0 讨论(0)
  • 2020-12-17 00:58

    Best way to configure swagger in your appliation is by first adding

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

    in pom.xml

    and then add plugin below plugin

    <plugin>
                    <groupId>com.github.kongchen</groupId>
                    <artifactId>swagger-maven-plugin</artifactId>
                    <version>3.1.5</version>
                    <configuration>
                        <apiSources>
                            <apiSource>
                                <springmvc>true</springmvc>
                                <locations>com.sandy2friends.test.web.rest</locations>
                                <info>
                                    <title>REST API</title>
                                    <version>1</version>
                                    <description>REST API</description>
                                </info>
                                <swaggerDirectory>swagger</swaggerDirectory>
                                <swaggerFileName>rest api</swaggerFileName>
                            </apiSource>
                        </apiSources>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>compile</phase>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    

    click here to know more on the plugin

    This configuration will automatically create swagger json file and will configure swagger ui in your application.

    0 讨论(0)
  • 2020-12-17 00:59

    Statement : Generate Swagger UI for the listing of all the REST APIs through Spring Boot Application.

    Follow the below steps to generate the Swagger UI through Spring Boot application:

    1. Add following dependency in pom.xml –

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

    2. Add the following piece of code in your main application class having the @EnableSwagger2 annotation.

        @EnableSwagger2
        @SpringBootApplication
        public class MyApp {
        public static void main(String[] args) {
            SpringApplication.run(MyApp.class, args);
        }
    
        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2).select()  
               .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
               .paths(PathSelectors.any()).build().pathMapping("/")
               .apiInfo(apiInfo()).useDefaultResponseMessages(false);
        }
    
        @Bean
        public ApiInfo apiInfo() {
            final ApiInfoBuilder builder = new ApiInfoBuilder();
            builder.title("My Application API through Swagger UI").version("1.0").license("(C) Copyright Test")
            .description("List of all the APIs of My Application App through Swagger UI");
            return builder.build();
            }
        }
    

    3. Add the below RootController class in your code to redirect to the Swagger UI page. In this way, you don’t need to put the dist folder of Swagger-UI in your resources directory.

        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;
        @Controller
        @RequestMapping("/")
        public class RootController {
            @RequestMapping(method = RequestMethod.GET)
            public String swaggerUi() {
                return "redirect:/swagger-ui.html";
            }
        }
    

    4. Being the final steps, add the @Api and @ApiOperation notation in all your RESTControllers like below –

        import static org.springframework.web.bind.annotation.RequestMethod.GET;
        import org.springframework.http.HttpStatus;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;
        import org.springframework.web.bind.annotation.ResponseStatus;
        import org.springframework.web.bind.annotation.RestController;
        import io.swagger.annotations.Api;
        import io.swagger.annotations.ApiOperation;
    
        @RestController
        @RequestMapping("/hello")
        @Api(value = "hello", description = "Sample hello world application")
        public class TestController {
    
            @ApiOperation(value = "Just to test the sample test api of My App Service")
            @RequestMapping(method = RequestMethod.GET, value = "/test")
            // @Produces(MediaType.APPLICATION_JSON)
            public String test() {
                return "Hello to check Swagger UI";
            }
    
            @ResponseStatus(HttpStatus.OK)
            @RequestMapping(value = "/test1", method = GET)
            @ApiOperation(value = "My App Service get test1 API", position = 1)
            public String test1() {
                System.out.println("Testing");
                if (true) {
                    return "Tanuj";
                }
                return "Gupta";
            }
        }
    

    Now your are done. Now to run your Spring Boot Application, go to browser and type localhost:8080. You will see Swagger UI having all the details of your REST APIs.

    Happy Coding.

    0 讨论(0)
提交回复
热议问题