接口集成Swagger-ui

项目集成Swagger-ui

在项目中,我们经常要提供很多接口,接口测试时要拿接口说明给前端,还要自己写个文档给他有点麻烦,但是集成了swagger-ui之后就变得智能。这里简单的说明一下当前公司使用的swagger.话不多说:
1.在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.swagger.properties文件:

packageToScan=com.xxx.visual.action
apiDescription=&#x7EFC;&#x5408;&#x5C55;&#x793A;&#x5E73;&#x53F0;
apiTitle=&#x5927;&#x6570;&#x636E;&#x56E2;&#x961F;&#x63A5;&#x53E3;&#x6587;&#x6863;
apiVersion=1.0.0
teamOfService=&#x5927;&#x6570;&#x636E;&#x56E2;&#x961F;
devMode=true
suffix=.htm

这里不泄露公司名称。用xxx代替。
3.在这个com.xxx.visual.action 里面表示这些接口都集成swagger4j:
在com.xxx.visual.swagger包里面:

package com.xxx.visual.swagger;

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.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author xxx
 * @date 2018/5/9 13:39
 */
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages = {"com.xxx.visual.action"})
public class SwaggerConfig{

    @Bean
    public Docket customDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("xxx", "xxx", "xxx");
        return new ApiInfoBuilder()
                .title("前台API接口")
                .description("前台API接口")
                .contact(contact)
                .version("1.1.0")
                .build();
    }
}

Contact实体类:

package springfox.documentation.service;

public class Contact {
    private final String name;
    private final String url;
    private final String email;

    public Contact(String name, String url, String email) {
        this.name = name;
        this.url = url;
        this.email = email;
    }

    public String getName() {
        return this.name;
    }

    public String getUrl() {
        return this.url;
    }

    public String getEmail() {
        return this.email;
    }
}

在拦截器不要拦截swagger-ui资源:

<mvc:exclude-mapping path="/**/**/**/*.css.htm"/><!-- swagger静态资源-->
<mvc:exclude-mapping path="/**/**/**/*.js.htm"/><!-- swagger静态资源-->

到这里``:
在这里插入图片描述
然后使用的时候:

@Controller
@RequestMapping(value="api/sale", name="票务收入分析接口")
@Api(value = "api/sale", description = "票务收入分析接口")
@RequestMapping(value = "getSaleTicketCountAndSum",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "beginDate", value = "开始时间", paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "endDate", value = "结束时间", paramType = "query", dataType = "string", required = true)
    })
    @ApiOperation(value="获取本期累计订单量和订单金额")
    @ResponseBody
    @ApiResponses( @ApiResponse(code=200, message = "", response=SaleVo.class, responseContainer="Map"))

SaleVo实体类说明

@ApiModel(value = "SaleVo")
public class SaleVo  {
    private static final long serialVersionUID = 1455834539998538766L;
    // ~~~~实体属性
    //累计订单量
    @ApiModelProperty(value = "累计订单量")
    private int saleCount;
    //累计订单金额
    @ApiModelProperty(value = "累计订单金额")
    private double saleSum;
    //同期累计订单量
    @ApiModelProperty(value = "同期累计订单量")
    private int lySaleCount;
    //同期累计订单金额
    @ApiModelProperty(value = "同期累计订单金额")
    private double lySaleSum;
    //数量
    @ApiModelProperty(value = "数量")
    private int num;
    //名称
    @ApiModelProperty(value = "名称")
    private String name;
    //单价
    @ApiModelProperty(value = "单价")
    private double price;
    //总金额
    @ApiModelProperty(value = "总金额")
    private double total;

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getTotal() {
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLySaleCount() {
        return lySaleCount;
    }

    public void setLySaleCount(int lySaleCount) {
        this.lySaleCount = lySaleCount;
    }

    public double getLySaleSum() {
        return lySaleSum;
    }

    public void setLySaleSum(double lySaleSum) {
        this.lySaleSum = lySaleSum;
    }

    public int getSaleCount() {
        return saleCount;
    }

    public void setSaleCount(int saleCount) {
        this.saleCount = saleCount;
    }

    public double getSaleSum() {
        return saleSum;
    }

    public void setSaleSum(double saleSum) {
        this.saleSum = saleSum;
    }
}

在这里插入图片描述
这样你就能愉快的使用swagger4j来玩了,访问地址:
http://localhost:8080/swagger-ui.html#!/

猜你喜欢

转载自blog.csdn.net/ZCCHZ/article/details/83501634