1.引入swagger依赖
<!-- swagger -->
<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>
2.增加swagger配置类
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.easyh.heeb.dispatch.controller")).paths(PathSelectors.any())
.build();
}
// 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 页面标题
.title("在线文档")
// 版本号
.version("1.0")
// 描述
.description("本文档项目在线文档")
.build();
}
3.给返回实体类增加描述
@ApiModel("返回实体")
public class Result<T> {
@ApiModelProperty("返回消息")
private String msg;
@ApiModelProperty("返回码")
private int code;
@ApiModelProperty("返回数量")
private int count;
@ApiModelProperty("返回内容")
private T data;
//返回成功结果
public static<T> Result<T> success(int count, T data) {
Result result=new Result();
result.setCode(ResponseEnum.SUCCESS.getStatus());
result.setMsg(ResponseEnum.SUCCESS.getMessage());
result.setCount(count);
result.setData(data);
return result;
}
//返回失败结果
public static<T> Result<T> fail(int count, T data) {
Result result=new Result();
result.setCode(ResponseEnum.FAIL.getStatus());
result.setMsg(ResponseEnum.FAIL.getMessage());
result.setCount(count);
result.setData(data);
return result;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public class MenuUrl {
@ApiModelProperty("链接ID")
private Integer id;
@ApiModelProperty("部门ID")
private Integer deptId;
@ApiModelProperty("链接名称")
private String businessName;
@ApiModelProperty("链接地址")
private String businessUrl;
@ApiModelProperty("图标地址")
private String picUrl;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public String getBusinessName() {
return businessName;
}
public void setBusinessName(String businessName) {
this.businessName = businessName;
}
public String getBusinessUrl() {
return businessUrl;
}
public void setBusinessUrl(String businessUrl) {
this.businessUrl = businessUrl;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
4.给接口类增加注解
5.给接口增加swagger注解
@RequestMapping("/findMenuUrl")
@ApiOperation(value = "findMenuUrl",tags = "菜单链接接口",httpMethod = "GET",consumes = "application/json",
produces = "application/json;charset=UTF-8",notes = "获取链接地址")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "userId", value = "用户ID" , dataType = "Integer"),
})
public Result<List<MenuUrl>> findMenuUrl(Integer userId){
List<MenuUrl> list=new ArrayList<>();
try {
log.info("findMenuUrl={}",userId);
User user=userService.getUserById(userId);
list=menuUrlService.getBusinessByDept(user.getDeptId());
return Result.success(list.size(),list);
}catch (Exception e){
return Result.fail(0,list);
}
}
6.启动项目,查看swagger在线接口http://localhost:8300/swagger-ui.html