SpringMVC + Swagger2 的使用
来源:互联网 发布:自己开淘宝店赚钱吗 编辑:程序博客网 时间:2024/06/11 14:40
SpringMVC + Swagger2 的使用
1.Swagger2 Jar 包的引入
maven pom.xml
<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>
2.创建 Swagger2 congfig Bean
package com.talent.consultingroom.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;@EnableWebMvc @EnableSwagger2 @ComponentScan(basePackages = {"com.talent.consultingroom.controller"}) @Configurationpublic class SwaggerApiConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(new ApiInfoBuilder().title("虚拟诊室接口API").version("0.1").build()) .select() .apis(RequestHandlerSelectors.basePackage("com.talent.consultingroom.controller")) .paths(PathSelectors.any()) .build(); } }
3.SpringMVC Swagger xml 配置
<bean class="com.talent.consultingroom.config.SwaggerApiConfig"/><mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/> <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
swagger-ui.html 的资源位置在 springfox-swagger-ui.jar 包中 所以要映入这一段配置
4.swagger 在controller 中的 使用
//描述接口@ApiOperation(value = "添加问诊依据")//描述参数 @ApiImplicitParams({ @ApiImplicitParam(name = "patient_id", value = "病人ID", required = true, dataType = "String", paramType = "path"), @ApiImplicitParam(name = "diag_id", value = "问诊ID", required = true, dataType = "String", paramType = "path"), @ApiImplicitParam(name = "diag_type", value = "问诊类型", required = true, dataType = "String", paramType = "path"), @ApiImplicitParam(name = "dispose_id_str", value = "处置ID 用 - 隔开", required = true, dataType = "String", paramType = "path") }) //描述接口返回信息 @ApiResponses({ @ApiResponse(code = 200,message="{'message':'0000','status':'success','data':'patient_flowinfo_list':[]}",response = PatientFlowInfo.class), @ApiResponse(code = 401,message="{'status': 'fail','message': '0002'}"), @ApiResponse(code = 403,message="{'status': 'fail','message': '0002'}"), @ApiResponse(code = 404,message="{'status': 'fail','message': '0002'}") }) @RequestMapping(value = "/diagPatient/{patient_id}/{diag_id}/{diag_type}/{dispose_id_str}", method = RequestMethod.GET) public Map<String, Object> diagPatient(@PathVariable("patient_id") String patient_id,@PathVariable("diag_id") String diag_id,@PathVariable("diag_type") int diag_type, @PathVariable("dispose_id_str") String dispose_id_str) throws Exception { return setDataValue(diagPatientService.diagPatient(patient_id, diag_id, diag_type, dispose_id_str)); }
5. swagger 接口返回结果 字段的描述 举例:
package com.talent.consultingroom.model;import java.io.Serializable;import io.swagger.annotations.ApiModel;import io.swagger.annotations.ApiModelProperty;@ApiModel(description = "病人消息流,病程的循环")public class PatientFlowInfo implements Serializable { /** * */ private static final long serialVersionUID = -6002706768078535623L; @ApiModelProperty("记录ID,每个处置唯一,且递增,但不连续") protected String record_id; protected String op_id; protected int status_id; protected String status_name; protected int record_type; protected String dispose_sub_type_name; protected int dispose_sub_type_id; protected String record_question; protected String record_answer; protected int result_type; protected String result_id; protected int health_score; protected String record_time; protected long time_cost; // get && set }
6.最后结果显示
http://localhost:8080/项目名/swagger-ui.html
1 0
- SpringMVC + Swagger2 的使用
- SpringMVC中使用Swagger2整合
- SpringMVC中使用FastJsonHttpMessageConverter时Swagger2失效的解决办法
- springmvc+swagger2整合
- Swagger2+SpringMVC配置详解
- SpringMvc 集成swagger2
- springmvc 整合 swagger2
- SpringMVC整合Swagger2
- SpringMVC集成swagger2
- SpringMVC整合swagger2
- SpringMVC集成Swagger2,初尝试和遇到的相关问题
- SpringMVC集成Swagger2,初尝试和遇到的相关问题
- SpringMvc 3分钟集成swagger2
- Spring Boot使用Swagger2
- spring boot 使用Swagger2
- 如何整合SpringMVC和Swagger2,并且使用Mock数据进行联调
- 在spring-mvc中使用swagger2遇到的问题汇总
- 使用Swagger2构建强大的RESTful API文档
- 第一章 Spring简史
- 安卓开发入门篇:Android Studio导入ApiDemos
- Bean property 'interceptorDao' is not writable or has an invalid setter method. Did you mean 'interc
- 如何开启Ubuntu的SSH服务
- android 滑动监听
- SpringMVC + Swagger2 的使用
- SQLServer2016 AlwaysOn基于证书的搭建笔记
- 蓝桥杯PREV-8 买不到的数目(简单dp)
- Apache Flume 1.6.0 用户指南
- linux-init命令运行级别
- Python PyImport_ImportModule() 或者 PyImport_Import()失败问题
- python进阶9:导入模块
- struct usb_endpoint_descriptor
- html中文乱码怎么改