依赖
<!-- swagger依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<!-- swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
配置文件
# knife4j增强
knife4j:
enable: true
配置类
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Tag;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.tags(new Tag("JsonPropertySerializationController", "JsonProperty相关测试"))
.select()
// 扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.wx.admin.controller"))
// 显示所有
.paths(PathSelectors.any())
// 只显示admin路径下的页面
// .paths(PathSelectors.regex("/admin/.*"))
.build();
}
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("管理后台")
//创建人
.contact(new Contact("程序员无羡", "https://blog.csdn.net/weixin_45427648?spm=1011.2266.3001.5343", "3220416593@qq.com"))
//版本号
.version("1.0")
//描述
.description("API 描述")
.build();
}
}
controller
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@PostMapping("/save")
@ApiOperation(value = "新增用户", notes = "作者:程序员无羡")
public ResultUtil<Boolean> savCompanyManage(@RequestBody UserDTO userDTO) {
return userService.saveUser(userDTO);
}
@GetMapping("/getById")
@ApiOperation(value = "根据编号查询用户", notes = "作者:程序员无羡")
public ResultUtil<UserVO> getById(@RequestParam(value = "userId") @ApiParam(value = "用户编号", name = "userId") Integer userId) {
return userService.getById(userId);
}
// @ApiImplicitParam和@ApiParam一回事
@ApiImplicitParams(
{
@ApiImplicitParam(name="username",value="用户名",required=true),
@ApiImplicitParam(name="password",value="密码",required=true)
}
)
@PostMapping("/login")
public AppResponse<UserRespVo> login(String username, String password){
//...
}
/**
* 分页数据
*/
@ApiOperation(value = "分页查询所有门店")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "path", name = "page", value = "当前页码", required = true, dataType = "Integer"),
@ApiImplicitParam(paramType = "path", name = "pageSize", value = "分大小", required = true, dataType = "Integer"),
@ApiImplicitParam(paramType = "query", name = "name", value = "名称", required = false, dataType = "String") })
@GetMapping(value = "/pageList/{page}/{pageSize}")
public PageVO<Store> findListByPage(@PathVariable int page,
@PathVariable int pageSize,
@RequestParam(value = "name",required = false) String name) {
return userService.queryPageByName(page, pageSize,name);
}
}
DTO
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "用户参数")
public class UserDTO {
@ApiModelProperty(value = "用户编号(新增时不用传)")
private Integer id;
// 默认非必传 request = false
@ApiModelProperty(value = "用户名称", required = true)
private String name;
@ApiModelProperty(value = "手机号", required = true)
private String mobile;
}
VO
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(value = "用户信息")
public class UserVO {
@ApiModelProperty(value = "用户编号")
private Integer id;
@ApiModelProperty(value = "用户名称")
private String name;
@ApiModelProperty(value = "手机号")
private String mobile;
}