用SpringCloud Alibaba搭建属于自己的微服务(八)~基础搭建~springboot整合swagger接口文档

一.概述

swagger接口文档是根据我们编写的controller自动生成,我只需要在接口上加上特定的注解,启动服务后,就直接可以在web页面上进行接口的调试.

二.springboot整合swagger

1.pom依赖

(1).ccm-mall.pom的 <dependencyManagement/>中加入以下依赖,声明版本号.

<!--swagger相关-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
    <exclusions>
        <exclusion>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-models</artifactId>
    <version>1.5.21</version>
</dependency>

(2).server.pom中加入以下依赖,实际引入

<dependency>
   <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
</dependency>
<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-models</artifactId>
</dependency>

2.代码配置

(1).springboot启动类上加@EnableSwagger2注解,代表开启swagger接口文档.

package com.ccm.server.user;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 *  @Description server-user服务启动类
 *  @Author zhouzhiwu
 *  @CreateTime 2020/07/03 14:04
 */
@EnableSwagger2
@MapperScan(basePackages = "com.ccm.server.user.dao.mysql.mapper")
@SpringBootApplication //声明为一个启动类
@Import(value = PaginationInterceptor.class)
public class ServerUserApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServerUserApplication.class,args);
    }
}

(2).新建swaggerConfig配置类.

package com.ccm.server.user.config;

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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @Description zhouzhiwu
 * @Author zhouzhiwu
 * @CreateTime 2020/7/8 17:38
 */
@Configuration
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ccm.server.user.controller"))    //swagger接口扫描包
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().version("1.0.0")
                .title("欢迎")
                .description("光临")
                .termsOfServiceUrl("www.baidu.com")
                .build();
    }
}

三.测试

1.代码内容

(1)SwaggerTestController.java

package com.ccm.server.user.controller;

import com.ccm.server.user.controller.req.SwaggerTestReq;
import com.ccm.server.user.controller.vo.SwaggerTestVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

/**
 *  @Description swagger整合测试
 *  @Author zhouzhiwu
 *  @CreateTime 2020/07/08 17:00
 */
@RestController
@RequestMapping(value = "swagger-test")
@Api(tags = "swagger测试")
public class SwaggerTestController {

    @ApiOperation(value = "rest风格接口测试之get请求")
    @GetMapping
    public SwaggerTestVO get(@ApiParam(value = "id",required = true) @RequestParam Long id) {
        SwaggerTestVO swaggerTestVO = new SwaggerTestVO();
        swaggerTestVO.setId(1L);
        swaggerTestVO.setName("zhouzhiwu");
        return swaggerTestVO;
    }

    @ApiOperation(value = "rest风格接口测试之post请求")
    @PostMapping
    public SwaggerTestVO post(@RequestBody SwaggerTestReq swaggerTestReq) {
        SwaggerTestVO swaggerTestVO = new SwaggerTestVO();
        swaggerTestVO.setId(1L);
        swaggerTestVO.setName("zhouzhiwu");
        return swaggerTestVO;
    }

    @ApiOperation(value = "rest风格接口测试之put请求")
    @PutMapping
    public SwaggerTestVO put(@RequestBody SwaggerTestReq swaggerTestReq) {
        SwaggerTestVO swaggerTestVO = new SwaggerTestVO();
        swaggerTestVO.setId(1L);
        swaggerTestVO.setName("zhouzhiwu");
        return swaggerTestVO;
    }

    @ApiOperation(value = "rest风格接口测试之dele请求")
    @DeleteMapping
    public SwaggerTestVO delete(@ApiParam(value = "id",required = true) @RequestParam Long id) {
        SwaggerTestVO swaggerTestVO = new SwaggerTestVO();
        swaggerTestVO.setId(1L);
        swaggerTestVO.setName("zhouzhiwu");
        return swaggerTestVO;
    }
}

(2).SwaggerTestReq.java

package com.ccm.server.user.controller.req;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 *  @Description swagger测试入参实体
 *  @Author zhouzhiwu
 *  @CreateTime 2020/07/08 17:01
 */
@ApiModel(value = "swagger测试入参实体")
@Data
public class SwaggerTestReq {

    @ApiModelProperty(value = "id")
    private Long id;

    @ApiModelProperty(value = "姓名")
    private String name;
}

(3).SwaggerTestVO.java

package com.ccm.server.user.controller.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 *  @Description swagger测试出参实体
 *  @Author zhouzhiwu
 *  @CreateTime 2020/07/08 17:01
 */
@ApiModel(value = "swagger测试出参实体")
@Data
public class SwaggerTestVO {

    @ApiModelProperty(value = "id")
    private Long id;

    @ApiModelProperty(value = "姓名")
    private String name;
}

2.启动服务

3.访问 localhost:1000/swagger-ui.html

(1)
在这里插入图片描述
(2)调用一个接口试试.
在这里插入图片描述访问成功.

源码地址:https://gitee.com/chouchimoo/ccm-mall.git(本章节分支:zj-8)

猜你喜欢

转载自blog.csdn.net/theOldCaptain/article/details/107211384