Swagger接口文档开发示例
2023年10月28日发(作者:入团申请书500字(精选23篇))
秋天的童话张杰现场版-
Swagger接⼝⽂档开发⽰例Maven依赖根据代码⽣成⽂档(必须)swagger2
//默认1.5.20版本,会出现类型转换异常问题,排除该版本,引⼊1.5.22版本
return new Docket(R_2) .globalOperationParameters(parameters) //传⼊全局配置 .apiInfo(apiInfo()) //传⼊api⾸页信息 .select() //进⾏筛选 //通过package筛选, .apis(ckage("ller")) //构建api .build(); }}Controller⽰例@Api(tags = "⽤户管理相关接⼝UserController")@RestController@RequestMapping("/user")public class UserController { @ApiOperation("获取⽤户信息") @ApiImplicitParams( { @ApiImplicitParam(name = "username", value = "⽤户名", dataType = "String", required = true ), @ApiImplicitParam(name = "password", value = "⽤户密码",dataType = "String",required = true) } ) @PutMapping("/updateUser") public String updateUser(String username,String password) { return username+","+password; } @PostMapping("/addUser") public User addUser(@RequestBody User user) { return user; } @GetMapping("/getUser") public String getUser(){ return "gcl"; } @DeleteMapping("/deleteUser") public String deledteUser(){ return "删除"; }实体类⽰例/* @ApiModel 对实体类⽣成⽂档*/@ApiModel/* @Data lombok插件,可以不选,那就需要⼿动⽣成构造⽅法和get,set⽅法*/@Datapublic class User { /* @ApiModelProperty 对属性进⾏配置 */ @ApiModelProperty(value = "⽤户id ",name = "idName",required = true) private int id; @ApiModelProperty(value = "⽤户名称 ",name = "nameName",required = true) private String name; @ApiModelProperty(value = "⽤户年龄 ",name = "ageName",required = true) private int age;}后记忽略通过@ApiIgnore 放在类前或⽅法前,则不扫描该类或⽅法通过RequestHandlerSelectors筛选RequestHandlerSelectors配置筛选规则有五个⽅法any() //所有none() //⽆withMethodAnnotation() //按照⽅法上的注解withClassAnnotation() //按照类上的注解basePackage() //按照包扫描使⽤⽅法.apis(ckage("ller"))通过PathSelectors筛选四个⽅法any() //所有none() //不扫描regex() //正则ant() //ant路径使⽤⽅法.paths(("/user/**"))