<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>2.7.0version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>2.7.0version>
dependency>
Swagger的属性配置
最重要的两个注解:@Configuration @EnableSwagger2
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
List<Parameter> pars = getParameters();
Docket docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
ApiSelectorBuilder selector = docket.select();
// api接口包扫描路径
List<Predicate<RequestHandler>> list = this.buildPackage();
if (list == null || list.isEmpty()) {
// 未配置,默认一个不存在的路径,避免扫描整个项目路径
selector.apis(RequestHandlerSelectors.basePackage("com.xxx.xxxxxx_.controller"));
} else if (list.size() == 1) {
selector.apis(list.get(0));//
} else if (list.size() > 1) {
selector.apis(Predicates.or(list));
}
//
selector.paths(PathSelectors.any())// 可以根据url路径设置哪些请求加入文档,忽略哪些请求
.build().globalOperationParameters(pars);
return docket;
}
public static List<Parameter> getParameters() {
ParameterBuilder tokenPar = new ParameterBuilder();
tokenPar.name("token").description("系统登录凭证").modelRef(new ModelRef("String")).parameterType("header").required(false);
List<Parameter> pars = new ArrayList<>();
pars.add(tokenPar.build());
return pars;
}
private List<Predicate<RequestHandler>> buildPackage() {
List<Predicate<RequestHandler>> result = new ArrayList<>();
String value = System.getProperties().getProperty("swagger-api-package");
if (value == null || value.trim().equals("")) {
return result;
}
String[] packageArr = value.split(";");
for (String str : packageArr) {
if (str == null || str.trim().equals("")) {
continue;
}
result.add(RequestHandlerSelectors.basePackage(str));
}
return result;
}
public ApiInfo apiInfo() {
return new ApiInfoBuilder().title("练习UI接口文档") 设置文档的标题
.description("了解更多请联系技术人员")// 设置文档的描述->1.Overview
.version("1.0")// 设置文档的版本信息
.contact(new Contact("史华昊", "https://blog.csdn.net/shihuahao0353?spm=1000.2115.3001.5343", "[email protected]"))
.termsOfServiceUrl("https://blog.csdn.net/shihuahao0353?spm=1000.2115.3001.5343")// 设置文档的License信息
.build();
}
}
多应用的项目中,controller的路径可能不止一个,在启动类里面加上下面代码:
System.getProperties().setProperty("swagger-api-package", "com.xxx.xxxxxx.controller");
在Swagger配置类中使用System.getProperties().getProperty(“swagger-api-package”)来获取到项目的controller路径。
Swagger的注解配置
@Api(value = "用户管理", tags= "用户管理API", protocols = "http")
@RestController
@RequestMapping(value = "/user")
public class UserController {
static Map<Long, User> users = Collections.synchronizedMap(new HashMap<>());
@ApiOperation(value = "获取用户列表", notes = "查询用户列表")
@RequestMapping(value = {""}, method = RequestMethod.GET)
@ApiResponses({
@ApiResponse(code = 100, message = "异常数据")
})
public List<User> getUserList() {
return new ArrayList<>(users.values());
}
@ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "query"),
@ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "age", value = "年龄", required = true, dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "ipAddr", value = "ip哟", required = false, dataType = "String", paramType = "query")
})
@RequestMapping(value = "", method = RequestMethod.POST)
public BaseResult<User> postUser(@ApiIgnore User user) {
users.put(user.getId(), user);
return BaseResult.successWithData(user);
}
@ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User getUser(@PathVariable Long id) {
return users.get(id);
}
@ApiOperation(value = "更新用户信息", notes = "根据用户ID更新信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "query"),
@ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "age", value = "年龄", required = true, dataType = "String", paramType = "query")
})
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public BaseResult<User> putUser(@PathVariable Long id, @ApiIgnore User user) {
User u = users.get(id);
u.setName(user.getName());
u.setAge(user.getAge());
users.put(id, u);
return BaseResult.successWithData(u);
}
}
/**
* 通用响应对象
*/
@ApiModel(description = "响应对象")
public class BaseResult<T> {
private static final int SUCCESS_CODE = 0;
private static final String SUCCESS_MESSAGE = "成功";
@ApiModelProperty(value = "响应码", name = "code", required = true, example = "" + SUCCESS_CODE)
private int code;
@ApiModelProperty(value = "响应消息", name = "msg", required = true, example = SUCCESS_MESSAGE)
private String msg;
@ApiModelProperty(value = "响应数据", name = "data")
private T data;
private BaseResult(int code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
private BaseResult() {
this(SUCCESS_CODE, SUCCESS_MESSAGE);
}
private BaseResult(int code, String msg) {
this(code, msg, null);
}
private BaseResult(T data) {
this(SUCCESS_CODE, SUCCESS_MESSAGE, data);
}
public static <T> BaseResult<T> success() {
return new BaseResult<>();
}
public static <T> BaseResult<T> successWithData(T data) {
return new BaseResult<>(data);
}
public static <T> BaseResult<T> failWithCodeAndMsg(int code, String msg) {
return new BaseResult<>(code, msg, null);
}
public static <T> BaseResult<T> buildWithParam(ResponseParam param) {
return new BaseResult<>(param.getCode(), param.getMsg(), null);
}
//getset方法
public static class ResponseParam {
private int code;
private String msg;
private ResponseParam(int code, String msg) {
this.code = code;
this.msg = msg;
}
public static ResponseParam buildParam(int code, String msg) {
return new ResponseParam(code, msg);
}
//getset方法
}
}
访问http://localhost:8080/swagger-ui.html就可以看到接口文档了~
1.controller的方法必须明确指定请求类型,缺少请求类型情况下列出所有请求类型。
2.Swagger在生产环境不能对外暴露,通过@Profile({“dev”, “prod”, “pre”})指定环境。
通过启动参数来规定这次启动是什么环境。
3.默认的访问地址需要携带默认访问路径,通过server.servlet.context-path指定默认访问路径。
访问http://localhost:8080/swagger/swagger-ui.html就可以了