官网:https://doc.xiaominfo.com/
GitHub:https://github.com/xiaoymin/swagger-bootstrap-ui
knife,简单翻译为小刀、匕首,从字面含义结合自身技术特性来说,确实实至名归,真正做到了小巧、轻量,并且功能强大,完美契合初中级程序员百分之八十的工作:增删改查(狗头)。
Knife4j一个是为了解决原始swagger-bootstrap-ui页面不美观,并且集成Swagger生成API文档而且可以在线测试接口的一种增强方案。
实现功能:
SpringBoot3.x版本暂无接触,以后可能会实现!
<dependency>
<groupId>com.github.xiaoymingroupId>
<artifactId>knife4j-spring-boot-starterartifactId>
<version>3.0.3version>
dependency>
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket createDocApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.groupName("接口文档")
.pathMapping("/")
.apiInfo(DocApi());
}
/**
* 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
*/
private ApiInfo DocApi() {
return new ApiInfoBuilder()
//页面标题
.title("接口测试工具")
//创建人
.contact(new Contact("", "", ""))
//版本号
.version("1.0")
//描述
.description("接口测试工具")
.build();
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
private static final String USER_API_DOC = ".*(\\/api/user\\/).*";
private static final String BACK_API_DOC = ".*(\\/api/admin\\/).*";
/**
* 接口地址中包含/api/user
*/
@Bean
public Docket createUserApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex(USER_API_DOC))
.build()
.groupName("用户端接口文档")
.pathMapping("/")
.apiInfo(new ApiInfoBuilder()
//页面标题
.title("用户端接口文档")
//创建人
.contact(new Contact("", "", ""))
//版本号
.version("2.0")
//描述
.description("用户端接口文档")
.build()
);
}
/**
* 接口地址中包含/api/admin
*/
@Bean
public Docket createBackEndApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex(BACK_API_DOC))
.build()
.groupName("管理端接口文档")
.pathMapping("/")
.apiInfo(new ApiInfoBuilder()
//页面标题
.title("管理端接口文档")
//创建人
.contact(new Contact("", "", ""))
//版本号
.version("2.0")
//描述
.description("管理端接口文档")
.build()
);
}
}
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping("/api/admin/coupon")
//接口文档一级标题
@Api(tags = "优惠券管理")
//接口作者,配置文件中 启动增强 才会生效
@ApiSupport(author = "张三")
public class CouponController {
@Resource
private WebCouponService webCouponService;
@GetMapping("/page")
//接口文档二级标题:真实接口
@ApiOperation(value = "查询分页列表", notes = "")
public ResultEntity<IPage<CouponPageResponse>> page(CouponPageRequest request) {
return ResultEntity.success(webCouponService.pageService(request));
}
}
knife4j:
# 增强功能开启
enable: true
# 开启生产环境屏蔽(该选项配置后,无法访问页面)
production: false
# 开启Swagger的Basic认证功能,默认是false,配置后登录才可访问页面
basic:
enable: true
# Basic认证用户名
username: admin
# Basic认证密码
password: 123456
@Slf4j
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(TestApplication.class);
Environment env = app.run(args).getEnvironment();
app.setBannerMode(Banner.Mode.CONSOLE);
logApplicationStartup(env);
}
private static void logApplicationStartup(Environment env) {
String protocol = "http";
if (env.getProperty("server.ssl.key-store") != null) {
protocol = "https";
}
String serverPort = env.getProperty("server.port");
String contextPath = env.getProperty("server.servlet.context-path");
if (StringUtils.isBlank(contextPath)) {
contextPath = "/doc.html";
} else {
contextPath = contextPath + "/doc.html";
}
String hostAddress = "localhost";
try {
hostAddress = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
log.warn("The host name could not be determined, using `localhost` as fallback");
}
log.info("\n----------------------------------------------------------\n\t" +
"Application '{}' is running! Access URLs:\n\t" +
"Local: \t\t{}://localhost:{}{}\n\t" +
"External: \t{}://{}:{}{}\n\t" +
"Profile(s): \t{}\n----------------------------------------------------------",
env.getProperty("spring.application.name"),
protocol,
serverPort,
contextPath,
protocol,
hostAddress,
serverPort,
contextPath,
env.getActiveProfiles());
}
}
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-boot-starterartifactId>
<version>3.0.0version>
dependency>
springfox中,去除swagger-annotations
和swagger-models
,并且重新引入1.5.21版本
代码如下:
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-boot-starterartifactId>
<version>3.0.0version>
<exclusions>
<exclusion>
<groupId>io.swaggergroupId>
<artifactId>swagger-annotationsartifactId>
exclusion>
<exclusion>
<groupId>io.swaggergroupId>
<artifactId>swagger-modelsartifactId>
exclusion>
<exclusion>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>io.swaggergroupId>
<artifactId>swagger-annotationsartifactId>
<version>1.5.21version>
dependency>
<dependency>
<groupId>io.swaggergroupId>
<artifactId>swagger-modelsartifactId>
<version>1.5.21version>
dependency>
解决方法:增加Spring MVC映射地址,配置类实现WebMvcConfigurer
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
在最近开发一个统计接口中,返回json数据中包含Bigdecimal的数据结构,整数的数据,直接把小数点和小数点后的零全部自动抹掉了,这个与实际的业务不符。
建议一些涉及到数字的接口,不要完全信任某个工具,可以再次用Postman测试一下,两个工具如果结果一样,才算接口返回正常,否则还需要再次修改。