- 1. 在
pom.xml
中加入Swagger2的依赖
1 <!--加入Swagger2的依赖--> 2 <dependency> 3 <groupId>io.springfox</groupId> 4 <artifactId>springfox-swagger2</artifactId> 5 <version>2.2.2</version> 6 </dependency> 7 <dependency> 8 <groupId>io.springfox</groupId> 9 <artifactId>springfox-swagger-ui</artifactId> 10 <version>2.2.2</version> 11 </dependency>
- 2. 在
Application.java
同级创建Swagger2的配置类Swagger2
1 import org.springframework.context.annotation.Bean; 2 import org.springframework.context.annotation.Configuration; 3 import springfox.documentation.builders.ApiInfoBuilder; 4 import springfox.documentation.builders.PathSelectors; 5 import springfox.documentation.builders.RequestHandlerSelectors; 6 import springfox.documentation.service.ApiInfo; 7 import springfox.documentation.spi.DocumentationType; 8 import springfox.documentation.spring.web.plugins.Docket; 9 import springfox.documentation.swagger2.annotations.EnableSwagger2; 10 11 12 @Configuration //让Spring来加载该类配置 13 @EnableSwagger2 //启用Swagger2 14 public class Swagger2 { 15 16 @Bean 17 public Docket createRestApi() { 18 return new Docket(DocumentationType.SWAGGER_2) 19 .apiInfo(apiInfo())//用来创建该Api的基本信息(这些基本信息会展现在文档页面中) 20 .select() 21 //用来控制哪些接口暴露给Swagger来展现, 22 //本例采用指定扫描的包路径来定义,Swagger会扫描该包下所有Controller定义的API,并产生文档内容 23 //(除了被@ApiIgnore指定的请求) 24 .apis(RequestHandlerSelectors.basePackage("com.example.demo1.web")) 25 .paths(PathSelectors.any()) 26 .build(); 27 } 28 29 private ApiInfo apiInfo() { 30 return new ApiInfoBuilder() 31 .title("Spring Boot中使用Swagger2构建RESTful APIs") 32 .description("更多Spring Boot相关文章请关注:https://www.cnblogs.com/yangjiming/category/1240129.html") 33 .termsOfServiceUrl("http://www.cnblogs.com/yangjiming/") 34 .contact("Mr.yang") 35 .version("1.0") 36 .build(); 37 } 38 }
- 3. 在代码中添加文档内容,通过@ApiOperation注解来给API增加说明、通过@ApiImplicitParams、@ApiImplicitParam注解来给参数增加说明。
1 import com.example.demo1.domain.User; 2 import io.swagger.annotations.ApiImplicitParam; 3 import io.swagger.annotations.ApiImplicitParams; 4 import io.swagger.annotations.ApiOperation; 5 import org.springframework.web.bind.annotation.*; 6 7 import java.util.*; 8 9 @RestController 10 @RequestMapping(value="/users") // 通过这里配置使下面的映射都在/users下 11 public class UserController { 12 13 static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>()); 14 15 @ApiOperation(value="获取用户列表", notes="") 16 @RequestMapping(value={""}, method=RequestMethod.GET) 17 public List<User> getUserList() { 18 List<User> r = new ArrayList<User>(users.values()); 19 return r; 20 } 21 22 @ApiOperation(value="创建用户", notes="根据User对象创建用户") 23 @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User") 24 @RequestMapping(value="", method=RequestMethod.POST) 25 public String postUser(@RequestBody User user) { 26 users.put(user.getId(), user); 27 return "success"; 28 } 29 30 @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息") 31 @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType="path") 32 @RequestMapping(value="/{id}", method=RequestMethod.GET) 33 public User getUser(@PathVariable Long id) { 34 return users.get(id); 35 } 36 37 @ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息") 38 @ApiImplicitParams({ 39 @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType="path"), 40 @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User") 41 }) 42 @RequestMapping(value="/{id}", method=RequestMethod.PUT) 43 public String putUser(@PathVariable Long id, @RequestBody User user) { 44 User u = users.get(id); 45 u.setName(user.getName()); 46 u.setAge(user.getAge()); 47 users.put(id, u); 48 return "success"; 49 } 50 51 @ApiOperation(value="删除用户", notes="根据url的id来指定删除对象") 52 @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long" , paramType="path") 53 @RequestMapping(value="/{id}", method=RequestMethod.DELETE) 54 public String deleteUser(@PathVariable Long id) { 55 users.remove(id); 56 return "success"; 57 } 58 }
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 }
- 4. 完成上述代码添加上,启动Spring Boot程序,访问:http://localhost:8080/swagger-ui.html
- 原文参考地址:http://blog.didispace.com/springbootswagger2/
原文地址:https://www.cnblogs.com/yangjiming/p/9213478.html
时间: 2024-10-07 16:55:57