Spring Boot 入门(七):集成 swagger2

本片文章是基于前一篇写的,《Spring Boot 入门(六):集成 treetable 和 zTree 实现树形图》,本篇主要介绍了spring boot集成swagger2。关于swagger的介绍,自行谷歌。我这里有在网上购买的相关视频资料,有需要可以呼叫我。

1.引入相关依赖

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

        <dependency>
            <groupId>org.apache.directory.studio</groupId>
            <artifactId>org.apache.commons.codec</artifactId>
            <version>1.8</version>
        </dependency>

很多地方只引入了前2个依赖,这里如果缺少第3个依赖,容易产生一个异常: java.lang.NoSuchMethodError: com.google.common.collect.FluentIterable.toList()Lcom/google/common/collect/ImmutableList;

 2.增加conf

import io.swagger.annotations.ApiOperation;
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;

/**
 * @program:
 * @description: Swagger配置
 * @author: DZ
 * @create: 2019-10-14 18:41
 **/
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 设置basePackage会将包下的所有类的所有方法作为api
//                .apis(RequestHandlerSelectors.basePackage("com.example.demo2.controller"))
                //只有标记了@ApiOperation的方法才会暴露出给swagger
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }


    private ApiInfo apiInfo() {
        //swagger2中termsOfServiceUrl方法已经启用,且contact的参数时一个对象,弃用字符串了
        Contact contact=new Contact("dz",
                "https://www.cnblogs.com/dz-boss/p/11729334.html","");
        return new ApiInfoBuilder()
                .title("xx项目说明文档")
                .description("xx系统说明")
                //.termsOfServiceUrl("https://www.cnblogs.com/dz-boss/p/11729334.html")
                .contact(contact)
                .version("1.0")
                .build();
    }

}

其中Contact构造函数中3个参数依次是:作者,地址,邮箱

 3.增加注解

import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @program:
 * @description: 
 * @author: DZ
 * @create: 2019-10-09 15:27
 **/
@Api(value = "API接口", tags = "Test", description = "API接口")
@Slf4j25 @Controller
public class Test {

    //测试接口
    @ApiOperation(value = "测试", notes = "测试接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "query", defaultValue = "123"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = false, dataType = "String", paramType = "query", defaultValue = "654")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Successful — 请求已完成"),
            @ApiResponse(code = 400, message = "请求中有语法问题,或不能满足请求"),
            @ApiResponse(code = 401, message = "未授权客户机访问数据"),
            @ApiResponse(code = 404, message = "服务器找不到给定的资源;文档不存在"),
            @ApiResponse(code = 500, message = "服务器不能完成请求")}
    )
    @ResponseBody
    @RequestMapping(value = "index", method = RequestMethod.POST)
    public String test(@RequestParam("id") String id, @RequestParam(value = "userId", required = false) String userId) {
        return "";
    }
}

 其中关键的注解为类的注解:@Api和方法的注解@ApiOperation,增加了这2个注解,就可以通过swagger的方式访问;

@ApiImplicitParams和@ApiResponses这2个注解主要对入参和出参进行增加中文注解,可有可无。

最后还需要在启动类增加注解@EnableSwagger2

 

相关推荐