功能和swagger類似
官網地址:https://doc.xiaominfo.com/knife4j/
這個框架可以設置返回字段的描述
引入依賴
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.7</version> </dependency>
Knife4jConfig .java
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.core.env.Profiles; import springfox.documentation.builders.ParameterBuilder; 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.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; import java.util.List; /** * @author yvioo。 */ @Configuration @EnableSwagger2 //開啟Swagger2 public class Knife4jConfig { /** * 配置Swagger的Docket的bean實例 * @return */ @Bean public Docket docket(Environment environment) { //設置只在開發中環境中啟動swagger Profiles profiles=Profiles.of("dev"); //表示如果現在是dev環境,則返回true 開啟swagger boolean flag=environment.acceptsProfiles(profiles); /*添加接口請求頭參數配置 沒有的話 可以忽略*/ ParameterBuilder tokenPar = new ParameterBuilder(); List<Parameter> pars = new ArrayList<>(); tokenPar.name("token").description("令牌").defaultValue("設置token默認值").modelRef(new ModelRef("string")).parameterType("header").required(false).build(); pars.add(tokenPar.build()); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) //是否啟動swagger 默認啟動 .enable(flag) //所在分組 .groupName("yvioo") .select() //指定掃描的包路徑 .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) //指定掃描的請求,這里表示掃描 /hello/ 的請求 //.paths(PathSelectors.ant("/hello/**")) .build() .globalOperationParameters(pars); } /** * 配置ApiInfo信息 * @return */ private ApiInfo apiInfo() { //作者信息 Contact author = new Contact("yvioo", "https://www.cnblogs.com/pxblog/", "111@qq.com"); return new ApiInfo( "Knife4j測試", "Knife4j描述", "1.0", "urn:tos", author, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList() ); } }
控制器的寫法和swagger基本類似
@Api(tags = "首頁模塊") @RestController public class IndexController { @ApiImplicitParam(name = "name",value = "姓名",required = true) @ApiOperation(value = "向客人問好") @GetMapping("/sayHi") public ResponseEntity<String> sayHi(@RequestParam(value = "name")String name){ return ResponseEntity.ok("Hi:"+name); } }
但是如果有其他配置繼承了 WebMvcConfigurationSupport 就需要增加資源映射 不然會失效
@Configuration public class WebMvcConfigurer extends WebMvcConfigurationSupport { /** * 發現如果繼承了WebMvcConfigurationSupport, 需要重新指定靜態資源 * */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations( "classpath:/static/"); registry.addResourceHandler("doc.html").addResourceLocations( "classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations( "classpath:/META-INF/resources/webjars/"); super.addResourceHandlers(registry); } }
效果
離線接口文檔
瀏覽器訪問
使用dev環境 啟動項目后 瀏覽器打開 http://localhost:8081/doc.html#/ 我這里用的端口是8081
整合swagger框架參考:https://www.cnblogs.com/pxblog/p/12942825.html