Maven引入
<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>
新建SwaggerConfig用于配置swagger
@Configuration
@EnableSwagger2
public class SwaggerConfig {
public Docket getDocket(){
ApiInfoBuilder apiInfoBuilder=new ApiInfoBuilder();
apiInfoBuilder.title("《xxx系统》后端接口说明")
.description("此文档说明了xxx系统项目后端接口规范……")
.version("v 2.0.1")
.contact(new Contact("BruceWong","www.brucejkd.com","brucewong@wankastudio.com"));
ApiInfo apiInfo =apiInfoBuilder.build();
Docket docket =new Docket(DocumentationType.SWAGGER_2);
docket.apiInfo(apiInfo) //指定生成文档中的封面信息
.select()
.apis(RequestHandlerSelectors.basePackage("com.bruce.controller")) //哪个包下面的类需要生成文档
.paths(PathSelectors.any()) //包下面的所有类都生成文档
// .paths(PathSelectors.regex("/user/")) //包下面的 带有“/user/” uri的请求类,生成文档
.build();
return docket;
}
}
给VO与实体类加swagger注释内容
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@ApiModel(value ="User对象", description ="用户/买家信息")
public class User {
@ApiModelProperty(dataType ="long", required =false)
private long userId;
@ApiModelProperty(dataType ="String", required =true, value ="用户名")
private String userName;
@ApiModelProperty(dataType ="String", required =true, value ="用户密码")
private String userPassword;
@ApiModelProperty(dataType ="String", required =true, value ="用户真是姓名")
private String userRealName;
@ApiModelProperty(dataType ="String", required =true, value ="用户头像url")
private String userImg;
@ApiModelProperty(dataType ="int", required =true, value ="用户年龄")
private int userAge;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@ApiModel(value ="响应的VO对象",description ="封装接口返回给前端的数据")
public class ResultVO {
@ApiModelProperty("响应状态码")
private int code;
@ApiModelProperty("响应提示信息")
private String msg;
@ApiModelProperty("响应数据内容")
private Object data;
}
在Controller中添加swagger注释内容
@Controller
@RequestMapping("/user")
@ResponseBody
@Api(value ="提供用户管理增删改查相关接口", tags ="用户管理")
public class UserController {
@Autowired
private UserService userService;
@ApiOperation("用户注册接口")
@ApiImplicitParam(name ="user", value ="用户注册信息", required =true)
@PostMapping("/register")
public User register(@RequestBody User user) {
userService.register(user);
return user;
}
@ApiOperation("用户登录接口")
@ApiImplicitParams({
@ApiImplicitParam(dataType ="string", name ="username", value ="用户登录账号", required =true),
@ApiImplicitParam(dataType ="string", name ="password", value ="用户登录密码", required =false, defaultValue ="111111")
})
@PostMapping("/checkLogin")
public ResultVO checkLogin(@RequestParam("username")String name,
@RequestParam(value ="password", defaultValue ="111111")String pwd) {
return userService.checkLogin(name, pwd);
}
}
运行查看文档
http://localhost:8080/swagger-ui.html
如果觉得文档界面不是自己喜欢的可以换一种显示方式
Maven引入
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
运行查看文档
http://localhost:8080/doc.html
网友评论