个人觉得swagger有点类似于阿里的hsf ops,只不过hsf ops是用于在线测试分布式服务,而swagger是用于在线测试rest api,感觉springboot集成swagger需要好多注解。。。。
0.在pom文件引入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifact>springfox-swagger2</artifact>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifact>springfox-swagger-ui</artifact>
<version>2.8.0</version>
</dependency>
1.引入配置
@EnableSwagger2
@Configuration
public class SwaggerConfig {
//是否开启swagger,正式环境一般是需要关闭的,可根据springboot的多环境配置进行设置
@Value(value = "${swagger.enabled}")
Boolean swaggerEnabled;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
// 是否开启
.enable(swaggerEnabled).select()
// 扫描的路径包
.apis(RequestHandlerSelectors.basePackage("cn.lqdev.learning.springboot.chapter10"))
// 指定路径处理PathSelectors.any()代表所有的路径
.paths(PathSelectors.any()).build().pathMapping("/");
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SpringBoot-Swagger2集成和使用-demo示例")
.description("qinlo| 趔趄的猿")
// 作者信息
.contact(new Contact("oKong", "https://blog.csdn.net/qq_30186661", "852925993@qq.com"))
.version("1.0.0")
.build();
}
}
3.在要测试的接口添加注解
@RestController
@RequestMapping(value ="/users")
@Api(tags = "用户API")
public class UserController {
static Map<Long,User> users= Collections.synchronizedMap(new HashMap<Long,User>());
@RequestMapping(value="/", method= RequestMethod.GET)
@ApiOperation(value = "获取用户列表")
public List<User> getUserList(){
List<User> list=new ArrayList<User>(users.values());
return list;
}
@RequestMapping(value="/",method = RequestMethod.POST)
@ApiOperation(value = "新增用户")
public String postUser(@Valid@ModelAttribute User user){
users.put(user.getId(),user);
return "success";
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ApiOperation(value = "获取用户")
public User getUser(@PathVariable Long id){
return users.get(id);
}
@ApiOperation(value = "修改用户")
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
@ApiImplicitParam(name = "id", value = "查询用户id", required = true)
public String putUser(@PathVariable Long id, @Valid@ModelAttribute User user){
User u=users.get(user.getId());
u.setName(user.getName());
u.setAge(user.getAge());
users.put(user.getId(),u);
return "success";
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@ApiOperation(value = "删除用户")
public String deleteUser(@PathVariable Long id){
users.remove(id);
return "success";
}
}
User.java
@ApiModel
public class User {
@NotEmpty
@ApiModelProperty(value = "id",dataType = "String",name="id",example = "111111")
private Long id;
@NotEmpty
@ApiModelProperty(value = "name",dataType = "String",name="name",example = "lq")
private String name;
@NotEmpty
@ApiModelProperty(value = "age",dataType = "int",name="age",example = "12")
private int age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}