Swagger
Springboot整合Swagger
在pom文件中添加依赖
io.springfox springfox-swagger2 2.2.2 io.springfox springfox-swagger-ui 2.2.2
配置Swagger
@Configurationpublic class Swagger2 { @Bean public Docket docket() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.chenbk.boot.controller")) //扫描controller所在的包 .paths(PathSelectors.any()) .build(); } /** * 创建页面的标题信息 * @return */ private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot中使用Swagger2小demo") .description("Spring Boot中使用Swagger2小demo") .termsOfServiceUrl("https://my.oschina.net/chenbkit") .contact("chenbk") .version("1.0") .build(); }}
给Contorller添加注解
@RestController@RequestMapping(value = "/users")public class UserController { private static Mapmap = new ConcurrentHashMap<>(); private static List list = Collections.synchronizedList(new ArrayList ()); static { User user = new User(); user.setId(1); user.setAge(18); user.setName("AA"); map.put(1, user); list.add(user); user = new User(); user.setId(2); user.setAge(28); user.setName("BB"); map.put(2, user); list.add(user); user = new User(); user.setId(3); user.setAge(38); user.setName("CC"); map.put(3, user); list.add(user); } @ApiOperation(value = "获取全部用户信息", notes = "获取全部用户信息") @RequestMapping(value = "", method = RequestMethod.GET) public List getAllUser() { return new ArrayList (map.values()); } @ApiOperation(value = "根据id获取用户信息", notes = "根据id获取用户信息") @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path") @RequestMapping(value = "/{id}", method = RequestMethod.GET) public User getUser(@PathVariable("id") Integer id) { return map.get(id); } @ApiOperation(value = "添加用户信息", notes = "添加用户信息") @RequestMapping(value = "", method = RequestMethod.POST) public String postUser(@ApiParam @RequestBody User user) { map.put(user.getId(), user); return "success"; } @ApiOperation(value = "修改用户信息", notes = "修改用户信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path") }) @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public String putUser(@PathVariable("id") Integer id,@ApiParam @RequestBody User user) { map.put(id, user); return "success"; } @ApiOperation(value = "删除用户信息",notes = "删除用户信息") @ApiImplicitParam(name ="id",value = "用户ID",required = true,dataType = "Integer",paramType = "path") @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) public String delUser(Integer id) { map.remove(id); return "success"; }}
@ApiModel(value="user对象",description="user对象")public class User implements Serializable { @ApiModelProperty(value = "用户ID",required = true,notes = "用户ID") private Integer id; @ApiModelProperty(value = "用户名称",required = true,notes = "用户名称") private String name; @ApiModelProperty(value = "用户年龄",required = true,notes = "用户年龄") private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}
测试
访问
访问
注解说明
作用范围 | 注解 | 使用位置 |
---|---|---|
对象属性 | @ApiModelProperty | 用在出入参数对象的字段上 |
协议集描述 | 用于controller类上 | |
协议描述 | @ApiOperation | 用在controller的方法上 |
Response集 | @ApiResponses | 用在controller的方法上 |
Response | @ApiResponse | 用在 @ApiResponses里边 |
非对象参数集 | @ApiImplicitParams | 用在controller的方法上 |
非对象参数描述 | @ApiImplicitParam | 用在@ApiImplicitParams的方法里边 |
描述返回对象的意义 | @ApiModel | 用在返回对象类上 |
@ApiImplicitParam
- paramType 查询参数类型 |值| 作用 | | :---: | :---:| | path | 以地址的形式提交数据 | | query |直接跟参数完成自动映射赋值 | | body | 以流的形式提交 仅支持POST | | header |参数在request headers 里边提交 | | form | 以form表单的形式提交 仅支持POST |
- dataType 参数的数据类型 只作为标志说明 Long String
- name 接收参数名
- value 接收参数的意义描述
- required 参数是否必填 true 必填 false 非必填
- defaultValue 默认值