第一步:添加Maven依赖
com.github.xiaoymin
knife4j-spring-boot-starter
3.0.3
第二步:修改application.yml文件
#配置swagger配置
knife4j:
# basic:
# username: admin
# password: 666666
# enable: true #开启认证
production: false #默认是false ,屏蔽所有Swagger的相关资源
enable: true #是否开启swagger
第三步:测试代码
Dept.java
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ApiModel(value = "Dept",description = "部门信息")
public class Dept implements Serializable {
private static final long serialVersionUID = -4383838089518165322L;
/**
* 部门编号
*/
@ApiModelProperty("部门编号")
private Integer deptno;
/**
* 部门名称
*/
@ApiModelProperty("部门名称")
private String dname;
/**
* 部门地址
*/
@ApiModelProperty("部门地址")
private String loc;
}
DeptController.java
@Api(tags = "用户信息管理", value = "用户接口")
@RestController
@RequestMapping("/user")
public class DeptController {
@ApiResponse(message = "测试", code = 200)
@ApiOperation(value = "fun", notes = "测试方法fun", httpMethod = "GET")
@GetMapping("/fun")
public String fun() {
return "fun";
}
@PutMapping(value = "/v1/{dname}")
@ApiOperation(value = "修改部门信息", notes = "", httpMethod = "PUT")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "String", dataTypeClass = String.class),
@ApiImplicitParam(paramType = "query", name = "deptno", value = "部门ID", required = true, dataType = "Integer", dataTypeClass = Integer.class),
@ApiImplicitParam(paramType = "path", name = "dname", value = "部门名称", required = true, dataType = "String", dataTypeClass = String.class),
@ApiImplicitParam(paramType = "body", name = "dept", value = "用户实体", required = true, dataType = "Dept", dataTypeClass = Dept.class)
})
public String fun(@RequestParam(name = "deptno", value = "deptno", required = false) Integer deptno,
@PathVariable(value = "dname", required = true) String dname,
@RequestBody(required = true) Dept dept,
HttpServletRequest request) {
String token = request.getHeader("token");
return token + " " + deptno + " " + dname + " " + dept;
}
}
测试
网址:http://localhost:8080/doc.html
效果: