Spring Swagger 配置

Swagger好文明!!

我再也不拿什么破python破postman调试了!!

第一步,引入依赖

1
2
3
4
5
6
7
8
9
10
<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.9.2,以后看到这篇文章的同学记得更新版本

第二步,配置SwaggerConfig类

  1. 新建一个package,叫config,便于管理
  2. 新建一个Java Class,叫SwaggerConfig
  3. 配置如下代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package xmu.oomall.user.config;//包名,可以修改

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;

/**
* @author Ringoer
* @notice 下面两个注解一定不要漏了
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {

//这个方法不可以修改
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}

//这个方法中所有的字符串都可以修改
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("UserInfoService RESTful API")
.description("3-1 oomall UserInfoService")
.termsOfServiceUrl("")
.version("1.0")
.build();
}
}

第三步,在主启动类上加入注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package xmu.oomall.user;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
* @author Ringoer
*/
@SpringBootApplication
@EnableSwagger2
public class UserApplication {

public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}

}

好了,直接运行,访问 http://{ip}:{port}/swagger-ui.html 即可看到好文明

可能出现的bug:

  1. 无法启动,报

    1
    compatible version of org.springframework.plugin.core.PluginRegistry

    如果出现这种情况,那就是因为依赖冲突,看看有没有下面这个依赖,有的话删掉即可

    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>

    要不然,可以建个空项目,慢慢调节找哪个依赖冲突也可以

    但反正绝对不是spring版本偏高,因为我现在运行的是2.2.2.RELEASE,也可以正常swagger

  2. 无法启动,报

    1
    NoSuchMethodError

    如果出现这种情况,那就是因为guava版本不对,需要手动指定guava版本

    1
    2
    3
    4
    5
    6
    <dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>20.0</version>
    <scope>compile</scope>
    </dependency>
  3. 可以启动,报一个弹窗:

    1
    Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:

    如果出现这种情况,那就是因为配了spring security或者用的不是 @EnableSwagger2 注解

    前者的话去掉security相关依赖,或者手动配置security config都可以

    后者的话请回到文章开头重新配置

--It's the end.Thanks for your read.--