Swagger的使用

Swagger 可以生成一个具有互动性的API控制台,开发者可以用来快速学习和尝试API

1
2
3
4
5
6
7
8
9
10
11
<!--pom-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>

Swagger配置类

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
@EnableSwagger2
@Configuration
public class Swagger2Configuration {

@Bean
public Docket accessToken() {
ParameterBuilder parameterBuilder = new ParameterBuilder();
List<Parameter> parameters = new ArrayList<Parameter>();
parameterBuilder.name("token")
.description("token")
.modelRef(new ModelRef("string"))
.parameterType("header")
.defaultValue("nbsjrb")
.required(false).build();
parameters.add(parameterBuilder.build());
return new Docket(DocumentationType.SWAGGER_2).groupName("api")
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.build()
.globalOperationParameters(parameters)
.apiInfo(apiInfo());
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Demo接口描述")
.description("swagger接口文档")
.build();
}
}

Model类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@ApiModel(value = "信息模型")
public class Mes {
@ApiModelProperty(value = "id",required = true)
private int id;
@ApiModelProperty(value = "姓名",required = true)
private String name;
@ApiModelProperty(value = "地址",required = true)
private String address;
@ApiModelProperty(value = "具体业务",required = true)
private String business;
@ApiModelProperty(value = "员工层级",required = true)
private String level;
//员工数量
@ApiModelProperty(value = "员工数量",required = true)
private String total;
//营业状态
@ApiModelProperty(value = "营业状态",required = true)
private String do_business;

错误问题
错误

注意事项

Spring boot启动项中不需要添加@EnableSwagger2,否则后台会一直在跑,Springcloud需要在启动项中添加该注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
//@EnableCaching 这个缓存用来实现@Cacheable 但是请注意,接口缓存不应该再这一层实现
@EnableEurekaClient
@EnableDiscoveryClient
//注解开启Feign功能
@EnableFeignClients
@EnableSwagger2
@EnableWebMvc
@EnableCaching
@ComponentScan(basePackages = {"com.nb.xry.consume.*"})
public class FinanceConsumeApplication {
public static void main(String[] args) {
SpringApplication.run(FinanceConsumeApplication.class, args);
}
}

解决方案

清理了chrome的缓存发现可以进入了

赏个🍗吧
0%