Swagger2依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
在使用时,发现2.7.2的比较好用,2.2.2的可能报错
配置文件
@Configuration @EnableSwagger2 //开启在线文档 public class SwaggerConfig { //1.声明api 文档的属性 构建起 private ApiInfo apiInfo() { return new ApiInfoBuilder().title("springboot使用在线文档构建RestFul风格Api") .description("bug猿") .termsOfServiceUrl("http://cnblogs.com/") .contact("java").version("1.0").build(); } //2配置核心配置信息 public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select().apis(RequestHandlerSelectors.basePackage("com.yuan.springbootdemo.controller")) .paths(PathSelectors.any()).build(); } }
修改Controller增加文档注释
@PutMapping("/{id}") @ApiOperation(value = "更新指定id用户信息",notes = "根据id更新用户信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Long"), @ApiImplicitParam(name = "user",value = "用户详细实体user",required = true,dataType = "User") }) public String updateUser(@PathVariable("id") Long id,User user) { for (User user2 : listUser) { if(user2.getId()==id) { user2.setName(user.getName()); user2.setAge(user.getAge()); } } return "success"; } //删除指定id用户 @DeleteMapping("/{id}") @ApiOperation(value="删除指定id用户信息",notes = "根据id删除") @ApiImplicitParam(name="id",value = "用户id",required = true,dataType = "Long") public String deleteUser(@PathVariable("id") Long id) { listUser.remove(getUser(id)); return "success"; } }
最后访问地址:http://localhost:8080/swagger-ui.html