springboot集成knife4j(swagger2)
一、在maven pom.xml文件中添加knife4j依赖
- 方式一、添加knife4j-starter只需要添加一个即可
<!--knife4j-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
- 方式二、添加swagger2依赖的话需要添加
<!--swagger2 start-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>${lastVersion}</version>
</dependency>
<!--swagger2 end-->
二、添加配置
package com.kangqiao.openapi.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
/**
* @author liouwb
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
Docket createRestApi() {
ResponseMessage requestParameterError = new ResponseMessageBuilder().code(400).message("Request Parameter Error").build();
ResponseMessage notFound = new ResponseMessageBuilder().code(404).message("Not Found").build();
ResponseMessage internalServerError = new ResponseMessageBuilder().code(500).message("Internal Server Error").build();
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.controller"))
.paths(PathSelectors.any())
.build()
.pathMapping("/")
// 以字符串代替日期格式显示
.directModelSubstitute(LocalDate.class, String.class)
.directModelSubstitute(LocalDateTime.class, String.class)
.genericModelSubstitutes(ResponseEntity.class)
// 页面显示信息
.apiInfo(apiInfo())
.enable(true)
// 设置全局自定义异常消息返回
.useDefaultResponseMessages(false)
.globalResponseMessage(RequestMethod.GET, Arrays.asList(requestParameterError, notFound, internalServerError))
.globalResponseMessage(RequestMethod.POST, Arrays.asList(requestParameterError, internalServerError))
.globalResponseMessage(RequestMethod.PUT, Arrays.asList(requestParameterError, internalServerError))
.globalResponseMessage(RequestMethod.OPTIONS, Arrays.asList(requestParameterError, internalServerError))
.globalResponseMessage(RequestMethod.DELETE, Arrays.asList(requestParameterError, internalServerError));
}
private ApiInfo apiInfo() {
return new ApiInfo("swagger2 API",
"swagger2 API 文档",
"1.0.0",
"/",
new Contact("swagger2 API文档", "/doc.html", ""),
"",
"",
new ArrayList<>());
}
}
三、在项目中使用swagger注解
- 在controller中和请求、返回实体类上加上swagger注解就可以
/**
* @author liouwb
*/
@RestController
@RequestMapping(value = "test", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
@Api(tags = "测试swagger")
public class TestController {
/**
* 获取access_token
*
* @param req
* @return
*/
@PostMapping("test")
@ApiOperation(value = "swagger测试")
public TestResp test(@RequestBody TestReq req) {
TestResp resp = new TestResp();
resp.setResult("hello world");
return resp;
}
}
- 请求类添加swagger注解
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* @author liouwb
*/
@ApiModel(value = "测试Swagger请求")
public class TestReq {
@ApiModelProperty(value = "测试字符串")
private String testString;
public String getTestString() {
return testString;
}
public void setTestString(String testString) {
this.testString = testString;
}
}
- 返回类添加swagger注解
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* @author liouwb
*/
@ApiModel(value = "测试Swagger请求")
public class TestResp {
@ApiModelProperty(value = "测试字符串")
private String result;
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}
- swagger注解类型
四、启动项目之后访问路径:http://${host}:${port}/doc.html
-
查看在线文档
-
导出离线文档
-
在线接口调试