spring boot使用Swagger2

Posted 华格瑞沙

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot使用Swagger2相关的知识,希望对你有一定的参考价值。

1. 在pom.xml中加入Swagger2的依赖

 <!--加入Swagger2的依赖-->
 <dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger2</artifactId>
     <version>2.2.2</version>
 </dependency>
 <dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger-ui</artifactId>
     <version>2.2.2</version>
  </dependency>                    

 

2. 在Application.java同级创建Swagger2的配置类Swagger2

 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import springfox.documentation.builders.ApiInfoBuilder;
 import springfox.documentation.builders.PathSelectors;
 import springfox.documentation.builders.RequestHandlerSelectors;
 import springfox.documentation.service.ApiInfo;
 import springfox.documentation.spi.DocumentationType;
 import springfox.documentation.spring.web.plugins.Docket;
 import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
 
 @Configuration //让Spring来加载该类配置
 @EnableSwagger2 //启用Swagger2
 public class Swagger2 {
 
     @Bean
     public Docket createRestApi() {
         return new Docket(DocumentationType.SWAGGER_2)
                 .apiInfo(apiInfo())//用来创建该Api的基本信息(这些基本信息会展现在文档页面中)
                 .select()
                 //用来控制哪些接口暴露给Swagger来展现,
                 //本例采用指定扫描的包路径来定义,Swagger会扫描该包下所有Controller定义的API,并产生文档内容
                 //(除了被@ApiIgnore指定的请求)
                 .apis(RequestHandlerSelectors.basePackage("com.example.demo1.web"))
                 .paths(PathSelectors.any())
                 .build();
     }
 
     private ApiInfo apiInfo() {
         return new ApiInfoBuilder()
                 .title("Spring Boot中使用Swagger2构建RESTful APIs")
                 .description("更多Spring Boot相关文章请关注:https://www.cnblogs.com/yangjiming/category/1240129.html")
                 .termsOfServiceUrl("http://www.cnblogs.com/yangjiming/")
                 .contact("Mr.yang")
                 .version("1.0")
                 .build();
     }
 }

 

3.  在代码中添加文档内容,通过@ApiOperation注解来给API增加说明、通过@ApiImplicitParams@ApiImplicitParam注解来给参数增加说明。

import com.example.demo1.domain.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
 @RequestMapping(value="/users")     // 通过这里配置使下面的映射都在/users下
 public class UserController {
 
     static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());
 
     @ApiOperation(value="获取用户列表", notes="")
     @RequestMapping(value={""}, method=RequestMethod.GET)
     public List<User> getUserList() {
         List<User> r = new ArrayList<User>(users.values());
         return r;
     }
 
     @ApiOperation(value="创建用户", notes="根据User对象创建用户")
     @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
     @RequestMapping(value="", method=RequestMethod.POST)
     public String postUser(@RequestBody User user) {
         users.put(user.getId(), user);
         return "success";
     }
 
     @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
     @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType="path")
     @RequestMapping(value="/{id}", method=RequestMethod.GET)
     public User getUser(@PathVariable Long id) {
         return users.get(id);
     }
 
     @ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
     @ApiImplicitParams({
             @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType="path"),
             @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
     })
     @RequestMapping(value="/{id}", method=RequestMethod.PUT)
     public String putUser(@PathVariable Long id, @RequestBody User user) {
         User u = users.get(id);
         u.setName(user.getName());
         u.setAge(user.getAge());
         users.put(id, u);
         return "success";
     }
 
     @ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
     @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long" , paramType="path")
     @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
     public String deleteUser(@PathVariable Long id) {
         users.remove(id);
         return "success";
     }
 }
 1 public class User {
 2 
 3     private Long id;
 4     private String name;
 5     private Integer age;
 6 
 7 
 8     public Long getId() {
 9         return id;
10     }
11 
12     public String getName() {
13         return name;
14     }
15 
16     public Integer getAge() {
17         return age;
18     }
19 
20     public void setId(Long id) {
21         this.id = id;
22     }
23 
24     public void setName(String name) {
25         this.name = name;
26     }
27 
28     public void setAge(Integer age) {
29         this.age = age;
30     }
31 }
View Code

 

4. 完成上述代码添加上,启动Spring Boot程序,访问:http://localhost:8080/swagger-ui.html

原文参考地址:http://blog.didispace.com/springbootswagger2/

以上是关于spring boot使用Swagger2的主要内容,如果未能解决你的问题,请参考以下文章

Spring boot集成swagger2

spring boot使用Swagger2

Spring-boot 之 Swagger2(打造不一样的api)

spring-boot实战06:Spring Boot中使用Swagger2

Spring Boot中使用Swagger2构建强大的RESTful API文档

java Spring Boot中使用Swagger2构建API文档