Swagger的使用
1、pom.xml
<!--Swagger-->
<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
2、配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket1(){return new Docket(DocumentationType.SWAGGER_2).groupName("A");}
@Bean
public Docket docket2(){return new Docket(DocumentationType.SWAGGER_2).groupName("B");}
@Bean
public Docket docket3(){return new Docket(DocumentationType.SWAGGER_2).groupName("C");}
/**
* 配置了Swagger的Docket的Bean实例
*/
@Bean
public Docket docket(Environment environment) {
//设置要显示Swagger的环境
Profiles profiles=Profiles.of("dev","test");
//通过environment.acceptsProfiles判断是否处在自己设定的环境当中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//是否开启Swagger
.enable(flag)
//分组 一般用于多人协同开发 自己配自己的Bean
.groupName("Shijx")
.select()
//RequestHandlerSelectors,配置要扫描接口的方式
//.basePackage("com.sjx.controller")指定要扫描的包
//.any(): 扫描全部
//.none(): 不扫描
//.withClassAnnotation(RestController.class): 扫描类上的注解,参数是一个注解的反射对象
//.withMethodAnnotation(GetMapping.class):扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.sjx.controller"))
//过滤.paths() 过滤什么路径
.paths(PathSelectors.ant("/test/**"))
.build();
}
/**
* 配置Swagger新的 apiInfo
*/
private ApiInfo apiInfo(){
//作者信息
Contact contact = new Contact("Helianthus","https://www.cnblogs.com/annuusl","shi12398@163.com");
return new ApiInfo(
"Swagger API文档",
"描述信息",
"1.0", "https://www.cnblogs.com/annuusl",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<VendorExtension>());
}
}
3、编写测试
package com.sjx.controller;
import com.sjx.pojo.User;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@ApiOperation("Hello控制器")
@GetMapping("/hello")
public String hello(){
return "Hello";
}
/**
* 只要我们的接口中,返回值存在实体类,他就会被扫描到Swagger中
* @return
*/
@GetMapping("/hello2")
public String hello2(@ApiParam("用户名") String username){
return "hello"+username;
}
@ApiOperation("测试POST")
@PostMapping("/post")
public User use2(@ApiParam("用户")User user){
int i= 5/0;
return user;
}
}
package com.sjx.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel("用户实体类")
@Data
public class User {
@ApiModelProperty("用户ID")
Integer id;
@ApiModelProperty("用户名")
String name;
@ApiModelProperty("密码")
String pwd;
}
4.application.yml
server:
port: 8081
spring:
profiles:
active: dev
server:
port: 8082
server:
port: 8080
Swagger的使用