RESTful API --Knife4j
作者:互联网
即使用swagger后今天又发现了一个基于swagger进行进一步封装的后端api Knife4j,其风格将swagger与postman完美结合,话不多说贴代码:
注意:
由于Knife4j是基于swagger来实现的RESTful api接口所以在引用Knife4j时需要将swagger引用的pom注释掉,否则在引用@EnableSwagger2WebMvc时将会引用失败。
Pom:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 需要将Swagger2核心包给注释掉再引用Knife4j --> <!--<!– Swagger2核心包 –>--> <!--<dependency>--> <!--<groupId>io.springfox</groupId>--> <!--<artifactId>springfox-swagger2</artifactId>--> <!--<version>3.0.0</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>io.springfox</groupId>--> <!--<artifactId>springfox-swagger-ui</artifactId>--> <!--<version>3.0.0</version>--> <!--</dependency>--> <!--knife4j api工具--> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.7</version> </dependency>
config:
package com.xxx.xxxxx.config; 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.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc; @Configuration @EnableSwagger2WebMvc public class Knife4jConfiguration { @Bean(value = "defaultApi2") public Docket defaultApi2() { Docket docket=new Docket(DocumentationType.SWAGGER_2) .apiInfo(new ApiInfoBuilder() //.title("swagger-bootstrap-ui-demo RESTful APIs") .description("# XXXXXX XXX RESTful APIs") .termsOfServiceUrl("http://ip:1010/项目名/")//接口地址 .contact("Y.C")//作者 .version("1.0")//版本 .build()) //分组名称 .groupName("1.0版本") .select() //这里指定Controller扫描包路径 .apis(RequestHandlerSelectors.basePackage("com.xxx.xxxx.controller")) .paths(PathSelectors.any()) .build(); return docket; } }
Controller:
package com.xxx.xxxx.controller; import com.xxx.xxxx.mapper.emapiiiMapper.CommonMapper; import com.xxx.xxxx.service.AstonMartinService; import com.xxx.xxxx.util.Tools; import com.github.xiaoymin.knife4j.annotations.ApiSupport; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; @RestController @RequestMapping(value = "/XXXXXXX ") @Slf4j @Api(tags = "XXXXXXX Api") //接口文档的api组名 @ApiSupport(order = 1)//接口组的自定义排序 可加可不加 public class XXXXXXXController { @Resource private CommonMapper commonMapper; @ApiImplicitParam(name = "na",value = "无",required = false)//参数 required是否必填 @ApiOperation(value = "测试接口")//接口释义 @GetMapping(value = "/v1/test") public String test() { try { String sysdate = commonMapper.getSysdate(); log.info( "--test--" + sysdate); return sysdate; } catch (Exception e) { log.error( "--test error:" + Tools.errorInfo(e)); e.printStackTrace(); } return "F"; } }
有时候提供给别人接口API时不希望所有人都能看到可以加一些登录限制可以在配置文件中启用增强模式
Application.yml/.properties配置如下:
################################# knife4j配置 ################################# knife4j.enable=true knife4j.basic.enable=true knife4j.basic.username=test knife4j.basic.password=111111
如果用户开启了basic认证功能,但是并未配置用户名及密码,Knife4j
提供了默认的用户名和密码:admin/123321
访问地址及效果图:
地址:http://ip:port/doc.html 例如:http://127.0.0.1:8080/doc.html
查看API前需要登录:
登录后查看api:
首页
具体接口及测试
查阅资料:
标签:Knife4j,swagger,--,knife4j,API,import,springfox,com 来源: https://www.cnblogs.com/yangchengdebokeyuan/p/16118528.html