swagger
依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
配置
SwaggerConfig
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//配置swagger的Docket的bean实例
@Bean
public Docket docket(Environment environment){
//设置要显示的swagger环境
Profiles profiles = Profiles.of("dev", "test");
//判断是否在已选中的环境中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//根据flag开关
.enable(flag)
//分组对应不同的Docket
.groupName("pinked")
//扫描路径
.select()
.apis(RequestHandlerSelectors.basePackage("cn.pinked.swagger.controller"))
//.paths(PathSelectors.ant("/过滤的路径/**"))
.build();
}
//配置swagger信息-apiinfo
public ApiInfo apiInfo(){
//contact存放作者信息
Contact contact = new Contact("野原新之助", "http://localhost:8080/", "abc@qq.com");
return new ApiInfo(
"swagger api文档",
"这里放描述信息",
"v1.0",
"http://localhost:8080/",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
yaml的多环境配置
spring:
profiles:
active: test
---
spring:
profiles: test
server:
port: 8080
---
spring:
profiles: prod
server:
port: 8081
---
spring:
profiles: dev
server:
port: 8082
接口注释
User
@ApiModel("用户实体类")
public class User {
@ApiModelProperty("用户名")
public String name;
@ApiModelProperty("密码")
public String password;
}
controller
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello swagger";
}
@PostMapping("/user")
public User user() {
return new User();
}
@ApiOperation("post测试")
@PostMapping("/postt")
public User postt(@ApiParam("用户") User user) {
int i = 5 / 0;
return user;
}
}
总结
- 可以为难理解的属性或接口添加注释信息
- 接口文档实时更新
- 可以在线测试