{"id":714,"date":"2023-10-31T09:01:01","date_gmt":"2023-10-31T01:01:01","guid":{"rendered":""},"modified":"2023-10-31T09:01:01","modified_gmt":"2023-10-31T01:01:01","slug":"knife4j\u63a5\u53e3\u6587\u6863\u5de5\u5177\u7684\u4f7f\u7528","status":"publish","type":"post","link":"https:\/\/mushiming.com\/714.html","title":{"rendered":"knife4j\u63a5\u53e3\u6587\u6863\u5de5\u5177\u7684\u4f7f\u7528"},"content":{"rendered":"

\u5de5\u5177\u5177\u4f53\u529f\u80fd\u5c31\u4e0d\u591a\u89e3\u91ca\u4e86\uff0c\u76f4\u63a5\u6b65\u5165\u4e3b\u9898<\/p>\n

\u5bfc\u5165\u76f8\u5173\u4f9d\u8d56<\/p>\n

  <dependency><\/span>\n    <groupId>com.github.xiaoymin<\/groupId><\/span>\n    <artifactId>knife4j-spring-boot-starter<\/artifactId><\/span>\n    <version>3.0.3<\/version><\/span>\n<\/dependency><\/span>\n<\/code><\/pre>\n

\u7136\u540e\u5728config\u6587\u4ef6\u5939\u91cc\u521b\u5efaSwaggerConfig\u7c7b\uff08\u540d\u79f0\u4e0d\u56fa\u5b9a\u53ea\u662f\u4e3a\u4e86\u65b9\u4fbf\u8bc6\u522b\u529f\u80fd\uff09<\/p>\n

package<\/span> com.example.demo.config;\n\nimport<\/span> com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;\nimport<\/span> org.springframework.context.annotation.Bean;\nimport<\/span> org.springframework.context.annotation.Configuration;\nimport<\/span> springfox.documentation.builders.ApiInfoBuilder;\nimport<\/span> springfox.documentation.builders.PathSelectors;\nimport<\/span> springfox.documentation.builders.RequestHandlerSelectors;\nimport<\/span> springfox.documentation.oas.annotations.EnableOpenApi;\nimport<\/span> springfox.documentation.service.Contact;\nimport<\/span> springfox.documentation.spi.DocumentationType;\nimport<\/span> springfox.documentation.spring.web.plugins.Docket;\nimport<\/span> springfox.documentation.swagger2.annotations.EnableSwagger2;\n\n@Configuration<\/span>\n@EnableOpenApi<\/span>\n\/\/ @EnableKnife4j<\/span>\n\/\/ @EnableSwagger2<\/span>\npublic<\/span> class<\/span> Knife4jConfig<\/span> {\n    @Bean<\/span>\n    public<\/span> Docket docket<\/span>()<\/span> {\n        Docket<\/span> docket<\/span> =<\/span> new<\/span> Docket<\/span>(DocumentationType.OAS_30)\n                .apiInfo(new<\/span> ApiInfoBuilder<\/span>()\n                        .title(\"\u6211\u7684\u6807\u9898\"<\/span>)\/\/\u4e0b\u9762\u51e0\u5904\u6587\u5b57\u90fd\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u9700\u8981\u66f4\u6539<\/span>\n                        .description(\"\u6211\u7684\u63cf\u8ff0\"<\/span>)\n                        \/\/ .termsOfServiceUrl(\"http:\/\/www.xx.com\/\")<\/span>\n                        .contact(new<\/span> Contact<\/span>(\"corder\"<\/span>, \"www.juejin.cn\"<\/span>, \"xx@qq.com\"<\/span>))\n                        .version(\"1.0\"<\/span>)\n                        .build())\n                \/\/ \u5206\u7ec4\u540d\u79f0<\/span>\n                .groupName(\"all\"<\/span>)\n                .select()\n                \/\/ \u8fd9\u91cc\u6307\u5b9aController\u626b\u63cf\u5305\u8def\u5f84\uff08\u6539\u4e3a\u81ea\u5df1\u7684Controller\u7c7b\u7684\u6587\u4ef6\u5939\uff09<\/span>\n                .apis(RequestHandlerSelectors.basePackage(\"com.example.demo\"<\/span>))\n                .paths(PathSelectors.any())\n                .build();\n\n        return<\/span> docket;\n    }\n}\n<\/code><\/pre>\n

\u4e00\u5b9a\u4e0d\u8981\u5fd8\u8bb0\u5728application.yml\u91cc\u8fdb\u884c\u8fd9\u9879mvc\u914d\u7f6e<\/strong><\/p>\n

  mvc:<\/span>\n    pathmatch:<\/span>\n      matching-strategy:<\/span> ANT_PATH_MATCHER<\/span>\n<\/code><\/pre>\n

\u989d\u5916\u914d\u7f6e\uff1a<\/p>\n

\u8fd8\u53ef\u4ee5\u5728Controller\u91cc\u6dfb\u52a0@Api(tags = \"\")\u548c @ApiOperation(\"\")\u6ce8\u89e3\u8ba9\u63a5\u53e3\u6587\u6863\u66f4\u6613\u8bfb\uff0c\u5982\u4e0b<\/p>\n

@RestController<\/span>\n@RequestMapping(\"\/user\")<\/span>\n@Api(tags = \"\u7528\u6237\")<\/span>\npublic<\/span> class<\/span> UserController<\/span> {\n    @Resource<\/span>\n    private<\/span> UserService userService;\n    \/\/\u7528\u6237\u6ce8\u518c<\/span>\n    @ApiOperation(\"\u6ce8\u518c\")<\/span>\n    @PostMapping(\"\/register\")<\/span>\n    public<\/span> BaseResponse<Long> userRegister<\/span>(@RequestBody<\/span> UserRegisterRequest registerRequest)<\/span>{\n    \n}\n\n<\/code><\/pre>\n

\"knife4j\u63a5\u53e3\u6587\u6863\u5de5\u5177\u7684\u4f7f\u7528<\/p>\n

\u8fd9\u65f6Controller\u7c7b\u548c\u65b9\u6cd5\u5c31\u6709\u4e86\u6613\u8bfb\u7684\u540d\u5b57<\/p>\n","protected":false},"excerpt":{"rendered":"knife4j\u63a5\u53e3\u6587\u6863\u5de5\u5177\u7684\u4f7f\u7528\u5de5\u5177\u5177\u4f53\u529f\u80fd\u5c31\u4e0d\u591a\u89e3\u91ca\u4e86\uff0c\u76f4\u63a5\u6b65\u5165\u4e3b\u9898 \u5bfc\u5165\u76f8\u5173\u4f9d\u8d56 \u7136\u540e\u5728config\u6587\u4ef6\u5939\u91cc\u521b\u5efaSwaggerConfig\u7c7b...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/714"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=714"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/714\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}