spring boot中整合swagger2

1、引入包

 1 <dependency>
 2             <groupId>io.springfox</groupId>
 3             <artifactId>springfox-swagger2</artifactId>
 4             <version>2.9.2</version>
 5         </dependency>
 6         <dependency>
 7             <groupId>io.springfox</groupId>
 8             <artifactId>springfox-swagger-ui</artifactId>
 9             <version>2.9.2</version>
10         </dependency>
swagger2依赖

2、添加spring boot web依赖包

1 <dependency>
2             <groupId>org.springframework.boot</groupId>
3             <artifactId>spring-boot-starter-web</artifactId>
4 </dependency>
web依赖

3、添加swagger配置文件,重写WebMvcConfigurer的addResourceHandlers方法是因为在整合后,访问接口文档页面提示404,找不到文件,所以这里要指定接口文档的静态文件地址。

 1 import org.springframework.context.annotation.Bean;
 2 import org.springframework.context.annotation.ComponentScan;
 3 import org.springframework.context.annotation.Configuration;
 4 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 5 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
 6 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 7 import springfox.documentation.builders.ApiInfoBuilder;
 8 import springfox.documentation.builders.PathSelectors;
 9 import springfox.documentation.service.ApiInfo;
10 import springfox.documentation.service.Contact;
11 import springfox.documentation.spi.DocumentationType;
12 import springfox.documentation.spring.web.plugins.Docket;
13 import springfox.documentation.swagger2.annotations.EnableSwagger2;
14 
15 @Configuration
16 @EnableSwagger2
17 @ComponentScan(basePackages = {"swagger2.demo"})
18 @EnableWebMvc
19 public class Swagger2Config implements WebMvcConfigurer{
20     @Bean
21     public Docket createRestApi() {
22         return new Docket(DocumentationType.SWAGGER_2)
23                 .apiInfo(apiInfo())
24                 .select()
25                 .paths(PathSelectors.any())
26                 .build();
27     }
28 
29 
30 
31     private ApiInfo apiInfo() {
32         Contact contact = new Contact("Rolay", "https://www.cnblogs.com/rolayblog/", "pengbenleihehe@outlook.com");
33         return new ApiInfoBuilder()
34                 .title("spring boot swagger2")
35                 .termsOfServiceUrl("https://www.cnblogs.com/rolayblog/")
36                 .description("接口文档示例")
37                 .contact(contact)
38                 .version("1")
39                 .build();
40 
41     }
42     @Override
43     public void addResourceHandlers(ResourceHandlerRegistry registry) {
44         registry.addResourceHandler("/**")
45                 .addResourceLocations("classpath:/static/");
46 
47         registry.addResourceHandler("swagger-ui.html")
48                 .addResourceLocations("classpath:/META-INF/resources/");
49 
50         registry.addResourceHandler("/webjars/**")
51                 .addResourceLocations("classpath:/META-INF/resources/webjars/");
52     }
53 
54 }
SwaggerConfig

4、添加controller,在这里要说一下,dont-speak方法我没有加swagger的注解。

 1 import io.swagger.annotations.ApiImplicitParam;
 2 import io.swagger.annotations.ApiOperation;
 3 import org.springframework.util.Assert;
 4 import org.springframework.util.StringUtils;
 5 import org.springframework.web.bind.annotation.GetMapping;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RestController;
 8 
 9 @RestController
10 @RequestMapping("/hi")
11 public class SayHiController {
12 
13     @ApiOperation(value="say hi", notes="say hi to name")
14     @ApiImplicitParam(name = "name", value = "name", paramType = "string", required = true)
15     @GetMapping("/hello")
16     public String sayHi(String name)
17     {
18         Assert.isTrue(!StringUtils.isEmpty(name),"name must be not null");
19         return  "hi "+name+"!nice to meet you!";
20     }
21     @GetMapping("/dont-speak")
22     public String dontSpeak(String name)
23     {
24         Assert.isTrue(!StringUtils.isEmpty(name),"name must be not null");
25         return "shut up! "+name+"!";
26     }
27 }
Controller

5、访问http://localhost:8080/swagger-ui.html

6、可以看到,生成了两个Controller的文档,而在实际的开发过程中可能dont-speak并不想在接口文档中显示,我们可以在配置类中做修改,自定义规则。

 1 import com.google.common.base.Predicate;
 2 import org.springframework.context.annotation.Bean;
 3 import org.springframework.context.annotation.ComponentScan;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 6 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
 7 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 8 import springfox.documentation.builders.ApiInfoBuilder;
 9 import springfox.documentation.service.ApiInfo;
10 import springfox.documentation.service.Contact;
11 import springfox.documentation.spi.DocumentationType;
12 import springfox.documentation.spring.web.plugins.Docket;
13 import springfox.documentation.swagger2.annotations.EnableSwagger2;
14 
15 import static com.google.common.base.Predicates.or;
16 import static springfox.documentation.builders.PathSelectors.regex;
17 
18 @Configuration
19 @EnableSwagger2
20 @ComponentScan(basePackages = {"swagger2.demo"})
21 @EnableWebMvc
22 public class Swagger2Config implements WebMvcConfigurer{
23     @Bean
24     public Docket createRestApi() {
25         return new Docket(DocumentationType.SWAGGER_2)
26                 .apiInfo(apiInfo())
27                 .select()
28                 .paths(paths())
29                 .build();
30     }
31 
32     private Predicate<String> paths() {
33 //        Predicate<String> paths=or(regex("/hi.*|/test.*"));
34         Predicate<String> paths=or(regex("/hi.hello"));
35         return paths;
36     }
37 
38     private ApiInfo apiInfo() {
39         Contact contact = new Contact("Rolay", "https://www.cnblogs.com/rolayblog/", "pengbenleihehe@outlook.com");
40         return new ApiInfoBuilder()
41                 .title("spring boot swagger2")
42                 .termsOfServiceUrl("https://www.cnblogs.com/rolayblog/")
43                 .description("接口文档示例")
44                 .contact(contact)
45                 .version("1")
46                 .build();
47 
48     }
49     @Override
50     public void addResourceHandlers(ResourceHandlerRegistry registry) {
51         registry.addResourceHandler("/**")
52                 .addResourceLocations("classpath:/static/");
53 
54         registry.addResourceHandler("swagger-ui.html")
55                 .addResourceLocations("classpath:/META-INF/resources/");
56 
57         registry.addResourceHandler("/webjars/**")
58                 .addResourceLocations("classpath:/META-INF/resources/webjars/");
59     }
60 
61 }
修改SwaggerConfig

可以看到规则使用正则表达式来匹配,可以根据自己的要求来修改。

原文地址:https://www.cnblogs.com/rolayblog/p/11287269.html