快速搭建swagger
在pom中写入如下依赖配置
springfox-swagger2
springfox-swagger-ui
在根目录下新建config文件夹
然后新建配置类Swagger2
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("程序的根目录"))
.paths(PathSelectors.any())
.build();
}private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("标题")
.description("详细说明")
.version("1.0")
.build();
}
}
在你需要的controller中输入基本的语句
@ApiOperation(value="https://www.it610.com/article/右边的值:注册", notes="注册用户",tags = "左边大标签",httpMethod = "可以不写 也可以写,如POST")
@ApiResponses(value = https://www.it610.com/article/{@ApiResponse(code = 200, message ="修改返回参数信息如成功", response = Void.class),
@ApiResponse(code = 400, message = "参数错误", response = Void.class),
@ApiResponse(code = 500, message = "Internal Server Error", response = Void.class)
})
如下:
@RestController
@RequestMapping("/user")
public class RegisterController {
@ApiOperation(value="https://www.it610.com/article/注册", notes="注册用户",tags = "注册用户",httpMethod = "POST")
@ApiResponses(value = https://www.it610.com/article/{@ApiResponse(code = 200, message ="成功", response = Void.class),
@ApiResponse(code = 400, message = "参数错误", response = Void.class),
@ApiResponse(code = 500, message = "Internal Server Error", response = Void.class)
})
@PostMapping(value = "https://www.it610.com/register/{name}")
public String postUser(@PathVariable("name") String name) {
return "hello"+name;
}
}
Swagger中参数说明
【快速搭建swagger】@Api 定义在类上,说明类的作用
@ApiOperation 定义在方法上,说明方法的作用
@ApiParam 定义在参数上,对参数进行设置 如默认值,是否必传等等
@ApiResponses 响应的参数集 包含一个或多个@ApiResponse
@ApiResponse 响应的参数 包含code 以及说明
@ApiModel 对返回类进行标记
@ApiModelProperty 对返回类中的属性进行标记说明
@ResponseHeader 返回头部 一般不常用
推荐阅读
- 六步搭建ES6语法环境
- (1)redis集群原理及搭建与使用(1)
- 桁架搭建有什么价值()
- neo4j|neo4j cql语句 快速查询手册
- VM|VM ware 的 harbor 私有仓库搭建 (Ubuntu16.04)
- PHP开发-Mac搭建ThinkPHP5.0
- 通过复盘快速成长(附模板)
- Python|Win10下 Python开发环境搭建(PyCharm + Anaconda) && 环境变量配置 && 常用工具安装配置
- sentry搭建错误监控系统(二)
- 【乐高创意作品】超智能!这款乐高搭建的除草机!