SpringMVC + Swagger2 的使用

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"})  
@Configuration
public 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
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值