Boot Spring Swagger2 used to build a powerful RESTful (the latest full, no pit)

1: Description

Too many of these articles online, a search a lot, but knowledge either too old, did not say the name of the configuration is clear, your project can not operate normally according to his configuration:

So the purpose of this article: Configuration swagger 2 that swagger 1 do not tell me about it, I feel no need, really need to build or maintain old project to jar package way, then refer to the following connection

https://github.com/swagger-api/swagger-ui/tree/2.x/dist   download content path, import dependence can be related, not recommended

 

2: swagger2 deployment 1

  2.1: import lib

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

 

  2.2 2: Create a configuration class

@Configuration
@EnableSwagger2
public class Swagger2 {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo (apiInfo ())
                .select()
                // scan package path 
                .apis (RequestHandlerSelectors.basePackage ( "com.dgw.controller" ))
                 // scan @APi labeled Class
                 // .apis (RequestHandlerSelectors.withClassAnnotation (ApiOperation.class)) 
                .paths (PathSelectors.any ( ))
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger2构建RESTful APIs")
                .description ( "Project" )
                .contact(new Contact("dgw", "https://www.cnblogs.com/dgwblog/", "[email protected]"))
                .version("1.0")
                .build();
    }
}

 

  2.3 Basically here online tutorials that let you start http: // localhost: 8080 / swagger-ui.html # access to and presentation API to get away Does he did not use interceptors Spring Boot access mapping your development projects?? It is a hello world? ha ha

  Here you must configure the resource mapping sping boot 2 in the configuration in webmvcconfigurationsupport

/**
 * Support webjars
 */
registry.addResourceHandler("/webjars/**")
        .addResourceLocations("classpath:/META-INF/resources/webjars/");
/**
 * Support swagger
 */
// 解决 SWAGGER 404报错
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");

 

  2.4 If your project is not used to show you can be successful interceptors accessible, but it is best to know needs to be configured interceptor

registry.addInterceptor(new LoginIntercepter()).addPathPatterns("/**")
        .excludePathPatterns("/user/login","/","/index")
           // swagger 排除规则
           .excludePathPatterns("/swagger-ui.html")
           .excludePathPatterns("/swagger-resources/**")
           .excludePathPatterns("/error")
           .excludePathPatterns("/webjars/**");

 

 

Visit at this time: there is no problem:

 

 

 

By the way, if your project uses a  spring security also need to exclude the following configuration

@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security", "/swagger-ui.html", "/webjars/**");
    }

}

 

 

 

3: swagger2 deployment recommendation 2

  Import lib '

<!-- https://mvnrepository.com/artifact/com.spring4all/swagger-spring-boot-starter -->
<dependency>
    <groupId>com.spring4all</groupId>
    <artifactId>swagger-spring-boot-starter</artifactId>
    <version>1.9.0.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

 

application.xml configuration

 

swagger.title=spring-boot-starter-swagger
swagger.description=Starter for swagger 2.x
swagger.version=1.4.0.RELEASE
swagger.license=Apache License, Version 2.0
swagger.licenseUrl=https://www.apache.org/licenses/LICENSE-2.0.html
swagger.termsOfServiceUrl=https://github.com/dyc87112/spring-boot-starter-swagger
swagger.contact.name=didi
swagger.contact.url = https: // www.cnblogs.com/dgwblog/ 
swagger.contact.email = [email protected]
# Scan package path
swagger.base-package=com.dgw.controller
swagger.base-path=/**

 

Start scanning configuration swagger

@SpringBootApplication this comment
@EnableSwagger2Doc
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

 

 

If there is a suggestion here to see the previous article, why not think about their own access.

 

Here write test

@Controller
@Api ( "Description Interface" )
 public  class HelloController in {
    @ApiOperation (value = "Hello method", notes = "return index" )
    @GetMapping("/hello")
    public String hello(){
        return  "index";
    }
}

 

It can be accessed:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/dgwblog/p/11965961.html