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
原创粉丝点击