Springboot工具篇03:Swagger的集成

       目前市面上存在很多接口测试的工具,jmeter,postman,swagger等等(我目前就使用过这几种)。swagger是最后才接触的,当我接触到swagger之后我觉得swagger更适合我自己的使用范围(另外两种可能对测试人员或者其他人来说更适合)。springboot能够快速的集成swagger。话不多说,直接来看集成步骤:

1. 在pom.xml文件中添加swagger2依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

2. 添加Swagger2配置

Swagger2.java

package com.example.demo001;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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
 * @date 2018/11/6 16:36
 * @description
 */
@Configuration
@EnableSwagger2
public class Swagger2 {
    /**
     * swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.demo001"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
     *
     * @return
     */
    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                //页面标题
                .title("测试Swagger")
                //创建人
                .contact(new Contact("张三丰", "http://www.baidu.com", "zhangsanfeng@123.com"))
                //版本号
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
        return apiInfo;
    }
}

3. 在Demo001Cntroller中添加相关注注解

代码如下

package com.example.demo001.controller;

import com.example.demo001.dto.OrderDetail;
import com.example.demo001.service.Demo001Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author
 * @date 2018/11/5 20:41
 * @description
 */
@Api("测试--Swagger说明")
@RestController
@RequestMapping("/demo")
public class Demo001Cntroller {

    @Autowired
    private Demo001Service demo001Service;

    //接口说明
    @ApiOperation(value = "查询订单详情--根据ID查询", notes = "查询某个订单详情")
    //参数说明
    //paramType的取值及含义如下
    //path --> /users/{id}
    //query --> /users?role=admin
    //header --> X-MyHeader: Value
    //cookie --> Cookie: debug=0; csrftoken=BUSe35dohU3O1MZvDCU
    @ApiImplicitParam(name = "detailId", value = "订单详情ID", paramType = "query", required = true, dataType = "String")
    @RequestMapping(value = "/getOrderDetail", method = {RequestMethod.GET, RequestMethod.POST})
    public OrderDetail getOrderDetail(@RequestParam("detailId") String detailId) {
        return demo001Service.getOrderDetailById(detailId);
    }
}

4. 测试

启动服务在浏览器中输入:http://localhost:8080/swagger-ui.html

点击 Try it out

 输入接口参数

 

 

 点击Execute

查看接口返回结果

 

 

到此完成

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值