swagger2
来源:互联网 发布:投资tw域名 编辑:程序博客网 时间:2024/06/05 04:02
swagger需要导入swagger相关页面,比较麻烦。
swagger2更近一步,只需要做几个配置即可,更简单,更易用。
项目基于spring-boot实现,下面指定配置点
pom.xml依赖
<!-- swagger 框架依赖 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency>
swagger2配置类
/** * * swagger2使用说明: @Api:用在类上,说明该类的作用 @ApiOperation:用在方法上,说明方法的作用 @ApiImplicitParams:用在方法上包含一组参数说明 @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面 paramType:参数放在哪个地方 header-->请求参数的获取:@RequestHeader query-->请求参数的获取:@RequestParam path(用于restful接口)-->请求参数的获取:@PathVariable body(不常用) form(不常用) name:参数名 dataType:参数类型 required:参数是否必须传 value:参数的意思 defaultValue:参数的默认值 @ApiResponses:用于表示一组响应 @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息 code:数字,例如400 message:信息,例如"请求参数没填好" response:抛出异常的类 @ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候) @ApiModelProperty:描述一个model的属性 * * */@Configuration@EnableSwagger2public class SwaggerConfig2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(createApiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.aaa.bbb")) .paths(PathSelectors.any()) .build(); } private ApiInfo createApiInfo() { return new ApiInfoBuilder().title("AAA RESTful API 说明文档") .version("1.0") .build(); }}
Application类中,配置如下
@EnableSwagger2@SpringBootApplication@EnableAutoConfigurationpublic class AAAApplication { public static void main(String[] args){ xxx.. }}
controller中
@Api(value = "aaa", description = "这是aaa")@RestController@RequestMapping("/aaa")@CrossOrigin(allowedHeaders="*", allowCredentials="true")public class AAAController { @RequestMapping(value = "/bbb", method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "bbb", notes = "这是bbb", httpMethod = "GET") public void getInstanceList(HttpServletResponse response, @ApiParam("ccc ID") @RequestParam(value = "ccc_id") int ccc_id){ sout。。。。}}
直接访问
http://服务名:8080/swagger-ui.html
即可
阅读全文
5 0
- swagger2
- swagger2
- Swagger2
- Spring4 & Swagger2
- SpringBoot -- Swagger2
- SpringBoot + Swagger2
- Swagger2用法
- Spring Boot 集成Swagger2
- Springboot集成swagger2
- Spring Boot集成Swagger2
- springmvc+swagger2整合
- spring-boot-swagger2 使用手册
- springboot+swagger2说明
- springboot接入swagger2
- Swagger2+SpringMVC配置详解
- SpringMVC + Swagger2 的使用
- springboot+swagger2说明
- springboot项目集成Swagger2
- 完整的定义事物属性
- 深入分析javaweb技术内幕笔记——深入分析Web请求过程
- coursera机器学习笔记(第一周、第二周)
- 树莓派(Raspberry Pi 3b)安装Kail Linux 2017.1
- Java正则环视和反向引用
- swagger2
- 晋晨瑞
- iOS图片拼接,图片文字合成总结
- MySQL两种安装方式之01_day02
- 安卓开发—自定义电池电量显示状态
- 作为产品新人,这三件事你都做到了吗?
- 第一个python程序
- 设置登入页面
- 剑指offer | 训练题44:扑克牌顺子