{"id":10006045,"date":"2022-07-28T17:53:11","date_gmt":"2022-07-28T09:53:11","guid":{"rendered":"https:\/\/ityet.com\/?p=10006045"},"modified":"2022-07-28T17:53:11","modified_gmt":"2022-07-28T09:53:11","slug":"%e4%b8%8d%e7%94%a8swagger%ef%bc%8c%e9%82%a3%e6%88%91%e7%94%a8%e5%95%a5%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006045","title":{"rendered":"\u4e0d\u7528Swagger\uff0c\u90a3\u6211\u7528\u5565\uff1f"},"content":{"rendered":"<p>@[toc]<br \/>\u4e0a\u5468\u677e\u54e5\u5199\u4e86\u4e00\u7bc7\u6587\u7ae0\u548c\u5c0f\u4f19\u4f34\u4eec\u5206\u4eab Swagger3 \u5728 Spring Boot \u4e2d\u7684\u7528\u6cd5\uff0c\u8bc4\u8bba\u4e2d\u6709\u4e0d\u5c11\u5c0f\u4f19\u4f34\u63a8\u8350 Spring Doc\uff0c\u677e\u54e5\u8d81\u7740\u4f11\u606f\u65f6\u95f4\u62bd\u7a7a\u770b\u4e86\u4e0b\uff0c\u8fd9\u4e2a\u4e1c\u897f\u786e\u5b9e\u4e0d\u9519\uff0c\u4e0d\u5b58\u5728\u548c Spring Boot \u4e4b\u95f4\u7684\u517c\u5bb9\u95ee\u9898\uff0c\u4e8e\u662f\u5c31\u64b8\u4e86\u8fd9\u7bc7\u6587\u7ae0\u548c\u5c0f\u4f19\u4f34\u4eec\u5206\u4eab\u3002\u4e00\u8d77\u6765\u770b\u770b\u8fd9\u4e2a\u597d\u73a9\u7684\u6587\u6863\u751f\u6210\u5de5\u5177\u5427\uff01<\/p>\n<h2>1. OpenApi<\/h2>\n<p>\u5728\u6b63\u5f0f\u5b66\u4e60 Spring Doc \u4e4b\u524d\uff0c\u5148\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e00\u4e0b OpenAPI\u3002<\/p>\n<p>OpenApi \u662f\u4e00\u4e2a\u4e1a\u754c\u7684 API \u6587\u6863\u6807\u51c6\uff0c\u662f\u4e00\u4e2a\u89c4\u8303\uff0c\u8fd9\u4e2a\u89c4\u8303\u76ee\u524d\u6709\u4e24\u5927\u5b9e\u73b0\uff0c\u5206\u522b\u662f\uff1a<\/p>\n<ul>\n<li>SpringFox<\/li>\n<li>SpringDoc<\/li>\n<\/ul>\n<p>\u5176\u4e2d SpringFox \u5176\u5b9e\u4e5f\u5c31\u662f\u6211\u4eec\u4e4b\u524d\u6240\u8bf4\u7684 Swagger\uff0cSpringDoc \u5219\u662f\u6211\u4eec\u4eca\u5929\u8981\u8bf4\u7684\u5185\u5bb9\u3002<\/p>\n<p>OpenApi \u5c31\u50cf JDBC \u4e00\u6837\uff0c\u5236\u5b9a\u4e86\u5404\u79cd\u5404\u6837\u7684\u89c4\u8303\uff0c\u800c Swagger \u548c SpringDoc \u5219\u7c7b\u4f3c\u4e8e\u5404\u79cd\u5404\u6837\u7684\u6570\u636e\u5e93\u9a71\u52a8\uff0c\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u3002<\/p>\n<p>\u6240\u4ee5\u53ef\u80fd\u5f88\u591a\u5c0f\u4f19\u4f34\u4e5f\u53d1\u73b0\u4e86\uff0cSwagger \u548c Spring Doc \u6709\u4e00\u4e9b\u76f8\u4f3c\u7684\u5730\u65b9\uff0c\u8fd9\u5c31\u662f\u56e0\u4e3a\u4ed6\u4eec\u90fd\u9075\u5b88\u4e86\u76f8\u540c\u7684\u89c4\u8303\u3002<\/p>\n<p>\u4e0d\u8fc7\u5462\uff0cSwagger \u66f4\u65b0\u6709\u70b9\u6162\u541e\u541e\u7684\uff0c\u4e3a\u4e86\u80fd\u591f\u548c\u65b0\u7248\u7684 Spring Boot \u6574\u5408\uff0c\u8fd8\u662f SpringDoc \u66f4\u503c\u5f97\u4f53\u9a8c\u4e00\u628a\u3002<\/p>\n<p>SpringDoc \u652f\u6301\uff1a<\/p>\n<ul>\n<li>OpenAPI 3<\/li>\n<li>Spring-boot\uff0c\u5168\u7248\u672c\u90fd\u652f\u6301\u3002<\/li>\n<li>JSR-303 \u4e2d\u63d0\u4f9b\u7684\u4e00\u4e9b\u6ce8\u89e3\uff0c\u4f8b\u5982 <code>@NotNull<\/code>\u3001<code>@Min<\/code>\u3001<code>@Max<\/code> \u4ee5\u53ca <code>@Size<\/code> \u7b49\u3002<\/li>\n<li>Swagger-ui\uff1aSpringDoc \u63d0\u4f9b\u7684\u63a5\u53e3 JSON \u4e5f\u53ef\u4ee5\u901a\u8fc7 Swagger-ui \u5c55\u793a\u51fa\u6765\u3002<\/li>\n<li>OAuth 2<\/li>\n<li>&#8230;<\/li>\n<\/ul>\n<h2>2. \u5f15\u5165 SpringDoc<\/h2>\n<p>\u5c0f\u4f19\u4f34\u4eec\u77e5\u9053\uff0c\u8fd9\u79cd\u751f\u6210\u63a5\u53e3\u6587\u6863\u7684\u5de5\u5177\uff0c\u4e00\u822c\u6765\u8bf4\u90fd\u662f\u4e24\u65b9\u9762\u7684\u529f\u80fd\uff1a<\/p>\n<ul>\n<li>\u751f\u6210\u63a5\u53e3\u6587\u6863 JSON\u3002<\/li>\n<li>\u6e32\u67d3\u63a5\u53e3\u6587\u6863 JSON\u3002<\/li>\n<\/ul>\n<p>\u6240\u4ee5\uff0c\u5f53\u6211\u4eec\u4f7f\u7528 SpringDoc \u7684\u65f6\u5019\uff0c\u5982\u679c\u53ea\u662f\u60f3\u8981\u751f\u6210\u63a5\u53e3\u6587\u6863 JSON\uff0c\u90a3\u4e48\u53ea\u9700\u8981\u6dfb\u52a0\u5982\u4e0b\u4f9d\u8d56\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springdoc&lt;\/groupId&gt;\n    &lt;artifactId&gt;springdoc-openapi-webmvc-core&lt;\/artifactId&gt;\n    &lt;version&gt;1.6.9&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u6b64\u65f6\uff0c\u5c31\u4f1a\u9488\u5bf9\u9879\u76ee\u4e2d\u7684\u63a5\u53e3\u81ea\u52a8\u751f\u6210\u63a5\u53e3\u7684 JSON \u6587\u6863\uff0c\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042233970\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u8fd9\u6837\u7684 JSON \u4fe1\u606f\u5f00\u53d1\u8005\u53ef\u4ee5\u81ea\u884c\u5c06\u4e4b\u7ed8\u5236\u51fa\u6765\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7f51\u4e0a\u4e00\u4e9b\u73b0\u6210\u7684\u5de5\u5177\u4f8b\u5982 Knife4j \u4e4b\u7c7b\u7684\u3002\u5f53\u7136\u4f60\u8981\u662f\u4e0d\u60f3\u8d39\u4e8b\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 SwaggerUI \u5c06\u4e4b\u7ed8\u5236\u51fa\u6765\uff0c\u5982\u679c\u60f3\u4f7f\u7528\u7f51\u9875\uff0c\u90a3\u4e48\u5c31\u4e0d\u8981\u4f7f\u7528\u4e0a\u9762\u7684\u4f9d\u8d56\uff0c\u7528\u4e0b\u9762\u8fd9\u4e2a\u4f9d\u8d56\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u751f\u6210 JSON \u63a5\u53e3\uff0c\u8fd8\u53ef\u4ee5\u751f\u6210\u6e32\u67d3\u540e\u7684\u7f51\u9875\uff1a<\/p>\n<pre><code class=\"xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springdoc&lt;\/groupId&gt;\n    &lt;artifactId&gt;springdoc-openapi-ui&lt;\/artifactId&gt;\n    &lt;version&gt;1.6.9&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u7f51\u9875\u6548\u679c\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042233971\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u8fd9\u4e2a\u7f51\u9875\u770b\u7740\u773c\u719f\uff0c\u5176\u5b9e\u5c31\u662f Swagger UI\u3002<\/p>\n<p>\u8fd9\u4e2a\u7f51\u9875\u4e0a\u6709\u4e00\u4e2a\u8f93\u5165\u6846\uff0c\u8f93\u5165\u7684\u5185\u5bb9\u662f <code>\/v3\/api-docs<\/code>\uff0c\u8fd9\u4e2a\u5730\u5740\u5c31\u662f\u8fd9\u4e2a\u7f51\u9875\u60f3\u8981\u6e32\u67d3\u7684 JSON \u7684\u5730\u5740\uff0c\u5982\u679c\u5f00\u53d1\u8005\u4fee\u6539\u4e86\u751f\u6210\u7684 JSON API \u6587\u6863\u7684\u5730\u5740\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u624b\u52a8\u5728\u8fd9\u4e2a\u8f93\u5165\u6846\u4e2d\u8f93\u5165\u4e00\u4e0b JSON API \u6587\u6863\u7684\u5730\u5740\u3002<\/p>\n<p>\u9ed8\u8ba4\u7684 JSON API \u6587\u6863\u5730\u5740\u662f\uff1a<\/p>\n<ul>\n<li>\/v3\/api-docs<\/li>\n<\/ul>\n<p>\u9ed8\u8ba4\u7684\u7f51\u9875 UI \u5730\u5740\u662f\uff1a<\/p>\n<ul>\n<li>\/swagger-ui\/index.html<\/li>\n<\/ul>\n<p>\u5982\u679c\u9700\u8981\u914d\u7f6e\uff0c\u5219\u53ef\u4ee5\u5728 Spring Boot \u7684 application.properties \u4e2d\u76f4\u63a5\u8fdb\u884c\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"properties\">springdoc.swagger-ui.path=\/javaboy-ui\nspringdoc.api-docs.path=\/javaboy-api<\/code><\/pre>\n<p>\u4e0d\u8fc7\u8fd9\u4e24\u4e2a\u914d\u7f6e\u5e76\u4e0d\u662f\u771f\u7684\u4fee\u6539\u4e86\u8bbf\u95ee\u8def\u5f84\uff0c\u8fd9\u4e24\u4e2a\u76f8\u5f53\u4e8e\u7ed9\u8bbf\u95ee\u8def\u5f84\u53d6\u4e86\u4e00\u4e2a\u522b\u540d\uff0c\u8bbf\u95ee\u8fd9\u4e24\u4e2a\u65f6\u4f1a\u81ea\u52a8\u91cd\u5b9a\u5411\u5230\u5bf9\u5e94\u7684\u8def\u5f84\u4e0a\u3002<\/p>\n<h2>3. \u7ed3\u5408 Spring Security<\/h2>\n<p>\u5982\u679c\u6211\u4eec\u7684\u9879\u76ee\u4e2d\u4f7f\u7528\u4e86 Spring Security\uff0c\u90a3\u4e48\u90e8\u5206\u63a5\u53e3\u7684\u53c2\u6570\u53ef\u80fd\u4f1a\u6bd4\u8f83\u7279\u6b8a\uff0c\u4f8b\u5982\u4e0b\u9762\u8fd9\u4e2a\u63a5\u53e3\uff1a<\/p>\n<pre><code class=\"java\">@RestController\npublic class HelloController {\n    @GetMapping(&quot;\/hello&quot;)\n    public String hello(@AuthenticationPrincipal User user) {\n        System.out.println(&quot;user = &quot; + user);\n        return &quot;hello&quot;;\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u63a5\u53e3\u7684\u53c2\u6570\u52a0\u4e0a\u4e86\u4e00\u4e2a <code>@AuthenticationPrincipal<\/code> \u6ce8\u89e3\u8868\u793a\u5f53\u524d\u767b\u5f55\u6210\u529f\u7684\u7528\u6237\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u53c2\u6570\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u5e76\u4e0d\u9700\u8981\u524d\u7aef\u4f20\u9012\uff0c\u670d\u52a1\u7aef\u4f1a\u81ea\u52a8\u6ce8\u5165\u8be5\u53c2\u6570\u3002<\/p>\n<p>\u4f46\u662f\uff01\u5982\u679c\u4f7f\u7528\u4e86 SpringDoc\uff0c\u901a\u8fc7\u7f51\u9875\u53bb\u8c03\u7528\u8fd9\u4e2a\u63a5\u53e3\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u53c2\u6570\u5c31\u5fc5\u987b\u8981\u8981\u4f20\u9012\uff0c\u5bf9\u4e8e\u8fd9\u79cd\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u5f15\u5165\u5982\u4e0b\u4f9d\u8d56\u81ea\u52a8\u5e2e\u6211\u4eec\u89e3\u51b3\uff1a<\/p>\n<pre><code class=\"xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springdoc&lt;\/groupId&gt;\n    &lt;artifactId&gt;springdoc-openapi-security&lt;\/artifactId&gt;\n    &lt;version&gt;1.6.9&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4f9d\u8d56\u4f1a\u81ea\u52a8\u5e2e\u6211\u4eec\u5ffd\u7565\u6389\u63a5\u53e3\u4e2d\u5e26\u6709 @AuthenticationPrincipal \u6ce8\u89e3\u7684\u53c2\u6570\uff0c\u8fd9\u6837\u6211\u4eec\u5728\u901a\u8fc7 swagger-ui \u53bb\u8fdb\u884c\u63a5\u53e3\u6d4b\u8bd5\u7684\u65f6\u5019\u5c31\u4e0d\u9700\u8981\u4f20\u9012\u8fd9\u4e2a\u53c2\u6570\u4e86\u3002<\/p>\n<h2>4. \u7ed3\u5408 Spring Data Rest<\/h2>\n<p>Spring Boot \u4e2d\u63d0\u4f9b\u4e86 Spring Data Rest\uff0c\u7ed3\u5408 Jpa \u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u7684\u6784\u5efa\u51fa Restful \u5e94\u7528\u3002\u4f46\u662f\u8fd9\u79cd Restful \u5e94\u7528\u4e0d\u9700\u8981\u5f00\u53d1\u8005\u81ea\u5df1\u5199\u63a5\u53e3\uff0c\u90a3\u4e48\u600e\u4e48\u751f\u6210\u63a5\u53e3\u6587\u6863\u5462\uff08\u8fde\u63a5\u53e3\u5728\u54ea\u91cc\u90fd\u4e0d\u77e5\u9053\uff09\uff1f\u9488\u5bf9\u4e8e\u6b64\uff0cSpringDoc \u4e5f\u63d0\u4f9b\u4e86\u76f8\u5173\u7684\u652f\u6301\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u4e0b\u3002<\/p>\n<h3>4.1 Spring Data Rest<\/h3>\n<h4>\u521b\u5efa\u5de5\u7a0b<\/h4>\n<p>\u9996\u5148\u521b\u5efa\u4e00\u4e2a Spring Boot \u5de5\u7a0b\uff0c\u5f15\u5165 <code>Web<\/code> \u3001 <code>Jpa<\/code> \u3001 <code>MySQL<\/code> \u3001<code>Rest Repositories<\/code> \u4f9d\u8d56\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042233972\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h4>\u914d\u7f6e\u6570\u636e\u5e93<\/h4>\n<p>\u4e3b\u8981\u914d\u7f6e\u4e24\u4e2a\uff0c\u4e00\u4e2a\u662f\u6570\u636e\u5e93\uff0c\u53e6\u4e00\u4e2a\u662f Jpa\uff1a<\/p>\n<pre><code class=\"properties\">spring.datasource.username=root\nspring.datasource.password=1234\nspring.datasource.url=jdbc:mysql:\/\/\/test02?serverTimezone=Asia\/Shanghai\n\nspring.jpa.hibernate.ddl-auto=update\nspring.jpa.show-sql=true\nspring.jpa.database=mysql\nspring.jpa.database-platform=mysql\nspring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect<\/code><\/pre>\n<p>\u8fd9\u91cc\u7684\u914d\u7f6e\uff0c\u548c Jpa \u4e2d\u7684\u57fa\u672c\u4e00\u81f4\u3002<\/p>\n<p>\u524d\u9762\u4e09\u884c\u914d\u7f6e\u4e86\u6570\u636e\u5e93\u7684\u57fa\u672c\u4fe1\u606f\uff0c\u5305\u62ec\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u3001\u6570\u636e\u5e93\u7528\u6237\u540d\u3001\u6570\u636e\u5e93\u5bc6\u7801\u3001\u6570\u636e\u5e93\u8fde\u63a5\u5730\u5740\u4ee5\u53ca\u6570\u636e\u5e93\u9a71\u52a8\u540d\u79f0\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u7684\u4e94\u884c\u914d\u7f6e\u4e86 JPA \u7684\u57fa\u672c\u4fe1\u606f\uff0c\u5206\u522b\u8868\u793a\u751f\u6210 SQL \u7684\u65b9\u8a00\u3001\u6253\u5370\u51fa\u751f\u6210\u7684 SQL \u3001\u6bcf\u6b21\u542f\u52a8\u9879\u76ee\u65f6\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u9009\u62e9\u662f\u5426\u66f4\u65b0\u8868\u3001\u6570\u636e\u5e93\u5e73\u53f0\u662f MySQL\u3002<\/p>\n<p>\u8fd9\u4e24\u6bb5\u914d\u7f6e\u662f\u5173\u4e8e MySQL + JPA \u7684\u914d\u7f6e\uff0c\u6ca1\u7528\u8fc7 JPA \u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u53c2\u8003\u677e\u54e5\u4e4b\u524d\u7684 JPA \u6587\u7ae0\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=VI20FCMY9rKtt1Xi0wVxHA%3D%3D.anm1pbj7sk3tKUCbbd2XU5l0CvClLvAH4TPgUiUJghlIZZ50Y1OAfbtegvh2NqcmmHZhXfE%2B4a5%2B0C8xI6kMZw%3D%3D\" rel=\"nofollow\">http:\/\/www.javaboy.org\/2019\/0407\/springboot-jpa.html<\/a><\/p>\n<h4>\u6784\u5efa\u5b9e\u4f53\u7c7b<\/h4>\n<pre><code class=\"java\">@Entity(name = &quot;t_book&quot;)\npublic class Book {\n    @Id\n    @GeneratedValue(strategy = GenerationType.IDENTITY)\n    private Long id;\n    @Column(name = &quot;book_name&quot;)\n    private String name;\n    private String author;\n    \/\/\u7701\u7565 getter\/setter\n}\npublic interface BookRepository extends JpaRepository&lt;Book,Long&gt; {\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u4e00\u4e2a\u662f\u914d\u7f6e\u4e86\u4e00\u4e2a\u5b9e\u4f53\u7c7b Book\uff0c\u53e6\u4e00\u4e2a\u5219\u662f\u914d\u7f6e\u4e86\u4e00\u4e2a BookRepository \uff0c\u9879\u76ee\u542f\u52a8\u6210\u529f\u540e\uff0c\u6846\u67b6\u4f1a\u6839\u636e Book \u7c7b\u7684\u5b9a\u4e49\uff0c\u5728\u6570\u636e\u5e93\u4e2d\u81ea\u52a8\u521b\u5efa\u76f8\u5e94\u7684\u8868\uff0cBookRepository \u63a5\u53e3\u5219\u662f\u7ee7\u627f\u81ea JpaRepository \uff0cJpaRepository \u4e2d\u81ea\u5e26\u4e86\u4e00\u4e9b\u57fa\u672c\u7684\u589e\u5220\u6539\u67e5\u65b9\u6cd5\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u4ee3\u7801\u5199\u5b8c\u4e86\u3002<\/p>\n<p>\u5565\uff1f\u4f60\u597d\u50cf\u5565\u90fd\u6ca1\u5199\u554a\uff1f\u662f\u7684\uff0c\u5565\u90fd\u6ca1\u5199\uff0c\u5565\u90fd\u4e0d\u7528\u5199\uff0c\u4e00\u4e2a RESTful \u98ce\u683c\u7684\u589e\u5220\u6539\u67e5\u5e94\u7528\u5c31\u6709\u4e86\uff0c\u8fd9\u5c31\u662f Spring Boot \u7684\u9b45\u529b\uff01<\/p>\n<h4>\u6d4b\u8bd5<\/h4>\n<p>\u6b64\u65f6\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u542f\u52a8\u9879\u76ee\u8fdb\u884c\u6d4b\u8bd5\u4e86\uff0c\u4f7f\u7528 POSTMAN \u6765\u6d4b\u8bd5\uff08\u5927\u5bb6\u4e5f\u53ef\u4ee5\u81ea\u884c\u9009\u62e9\u8d81\u624b\u7684 HTTP \u8bf7\u6c42\u5de5\u5177\uff09\u3002<\/p>\n<p>\u6b64\u65f6\u6211\u4eec\u7684\u9879\u76ee\u5df2\u7ecf\u9ed8\u8ba4\u5177\u5907\u4e86\u4e00\u4e9b\u63a5\u53e3\uff0c\u6211\u4eec\u5206\u522b\u6765\u770b\uff1a<\/p>\n<h6>\u6839\u636e id \u67e5\u8be2\u63a5\u53e3<\/h6>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=%2BrwV%2Fy6lNh%2BoFx%2B%2BXRKFTA%3D%3D.WZ%2BnjXhzoKzADnbeXcI6sAIlcwY5Q%2F4BP5DVBJUL%2BqY%3D\" rel=\"nofollow\">http:\/\/127.0.0.1:8080\/books\/<\/a>{id}<\/li>\n<\/ul>\n<p>\u8fd9\u4e2a\u63a5\u53e3\u8868\u793a\u6839\u636e id \u67e5\u8be2\u67d0\u4e00\u672c\u4e66\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042233973\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h6>\u5206\u9875\u67e5\u8be2<\/h6>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=u3lnd6sqv46h5j418ji4dg%3D%3D.jwfkKgZzT4jc7nv4NfcwJHio%2FnEtWjgWb%2FgxFaWVNpk%3D\" rel=\"nofollow\">http:\/\/127.0.0.1:8080\/books<\/a><\/li>\n<\/ul>\n<p>\u8fd9\u662f\u4e00\u4e2a\u6279\u91cf\u67e5\u8be2\u63a5\u53e3\uff0c\u9ed8\u8ba4\u8bf7\u6c42\u8def\u5f84\u662f\u7c7b\u540d\u9996\u5b57\u6bcd\u5c0f\u5199\uff0c\u5e76\u4e14\u518d\u52a0\u4e00\u4e2a s \u540e\u7f00\u3002\u8fd9\u4e2a\u63a5\u53e3\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u5206\u9875\u67e5\u8be2\u63a5\u53e3\uff0c\u6ca1\u6709\u4f20\u53c2\u6570\uff0c\u8868\u793a\u67e5\u8be2\u7b2c\u4e00\u9875\uff0c\u6bcf\u9875 20 \u6761\u6570\u636e\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042233974\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u67e5\u8be2\u7ed3\u679c\u4e2d\uff0c\u9664\u4e86\u8be5\u6709\u7684\u6570\u636e\u4e4b\u5916\uff0c\u4e5f\u5305\u542b\u4e86\u5206\u9875\u6570\u636e\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042233975\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5206\u9875\u6570\u636e\u4e2d\uff1a<\/p>\n<ol>\n<li>size \u8868\u793a\u6bcf\u9875\u67e5\u8be2\u8bb0\u5f55\u6570<\/li>\n<li>totalElements \u8868\u793a\u603b\u8bb0\u5f55\u6570<\/li>\n<li>totalPages \u8868\u793a\u603b\u9875\u6570<\/li>\n<li>number \u8868\u793a\u5f53\u524d\u9875\u6570\uff0c\u4ece0\u5f00\u59cb\u8ba1<\/li>\n<\/ol>\n<p>\u5982\u679c\u8981\u5206\u9875\u6216\u8005\u6392\u5e8f\u67e5\u8be2\uff0c\u53ef\u4ee5\u4f7f\u7528 _links \u4e2d\u7684\u94fe\u63a5\u3002<code>http:\/\/127.0.0.1:8080\/books?page=1&amp;size=3&amp;sort=id,desc<\/code> \u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042233976\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h6>\u6dfb\u52a0<\/h6>\n<p>\u4e5f\u53ef\u4ee5\u6dfb\u52a0\u6570\u636e\uff0c\u6dfb\u52a0\u662f POST \u8bf7\u6c42\uff0c\u6570\u636e\u901a\u8fc7 JSON \u7684\u5f62\u5f0f\u4f20\u9012\uff0c\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042233977\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6dfb\u52a0\u6210\u529f\u4e4b\u540e\uff0c\u9ed8\u8ba4\u4f1a\u8fd4\u56de\u6dfb\u52a0\u6210\u529f\u7684\u6570\u636e\u3002<\/p>\n<h6>\u4fee\u6539<\/h6>\n<p>\u4fee\u6539\u63a5\u53e3\u9ed8\u8ba4\u4e5f\u662f\u5b58\u5728\u7684\uff0c\u6570\u636e\u4fee\u6539\u8bf7\u6c42\u662f\u4e00\u4e2a PUT \u8bf7\u6c42\uff0c\u4fee\u6539\u7684\u53c2\u6570\u4e5f\u662f\u901a\u8fc7 JSON \u7684\u5f62\u5f0f\u4f20\u9012\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042233978\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4fee\u6539\u6210\u529f\u540e\uff0c\u4f1a\u8fd4\u56de\u4fee\u6539\u6210\u529f\u7684\u6570\u636e\u3002<\/p>\n<h6>\u5220\u9664<\/h6>\n<p>\u5f53\u7136\u4e5f\u53ef\u4ee5\u901a\u8fc7 DELETE \u8bf7\u6c42\u6839\u636e id \u5220\u9664\u6570\u636e\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042233979\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5220\u9664\u6210\u529f\u540e\uff0c\u662f\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u3002<\/p>\n<p>\u4e0d\u9700\u8981\u51e0\u884c\u4ee3\u7801\uff0c\u4e00\u4e2a\u57fa\u672c\u7684\u589e\u5220\u6539\u67e5\u5c31\u6709\u4e86\u3002<\/p>\n<p>\u8fd9\u4e9b\u90fd\u662f\u9ed8\u8ba4\u7684\u914d\u7f6e\uff0c\u8fd9\u4e9b\u9ed8\u8ba4\u7684\u914d\u7f6e\u5b9e\u9645\u4e0a\u90fd\u662f\u5728 JpaRepository \u7684\u57fa\u7840\u4e0a\u5b9e\u73b0\u7684\uff0c\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5bf9\u8fd9\u4e9b\u529f\u80fd\u8fdb\u884c\u5b9a\u5236\u3002<\/p>\n<h4>\u67e5\u8be2\u5b9a\u5236<\/h4>\n<p>\u6700\u5e7f\u6cdb\u7684\u5b9a\u5236\uff0c\u5c31\u662f\u67e5\u8be2\uff0c\u56e0\u4e3a\u589e\u5220\u6539\u64cd\u4f5c\u7684\u53d8\u5316\u4e0d\u50cf\u67e5\u8be2\u8fd9\u4e48\u4e30\u5bcc\u3002\u5bf9\u4e8e\u67e5\u8be2\u7684\u5b9a\u5236\uff0c\u975e\u5e38\u5bb9\u6613\uff0c\u53ea\u9700\u8981\u63d0\u4f9b\u76f8\u5173\u7684\u65b9\u6cd5\u5373\u53ef\u3002\u4f8b\u5982\u6839\u636e\u4f5c\u8005\u67e5\u8be2\u4e66\u7c4d\uff1a<\/p>\n<pre><code class=\"java\">public interface BookRepository extends JpaRepository&lt;Book,Long&gt; {\n    List&lt;Book&gt; findBookByAuthorContaining(@Param(&quot;author&quot;) String author);\n}<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u65b9\u6cd5\u7684\u5b9a\u4e49\uff0c\u53c2\u6570\u8981\u6709 @Param \u6ce8\u89e3\u3002<\/p>\n<p>\u5b9a\u5236\u5b8c\u6210\u540e\uff0c\u91cd\u542f\u9879\u76ee\uff0c\u6b64\u65f6\u5c31\u591a\u4e86\u4e00\u4e2a\u67e5\u8be2\u63a5\u53e3\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7 <a href=\"https:\/\/link.segmentfault.com\/?enc=z8on5CKIZlhgoozfBJ%2BW7A%3D%3D.8NjDFR3apQeG516l6Vp8G1sgvR98UCnUdKpEePZeWYYOCrfDQVA%2By6t9XckWgxSK\" rel=\"nofollow\">http:\/\/localhost:8080\/books\/s&#8230;<\/a> \u6765\u67e5\u770b\u548c book \u76f8\u5173\u7684\u81ea\u5b9a\u4e49\u63a5\u53e3\u90fd\u6709\u54ea\u4e9b\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042233980\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u67e5\u8be2\u7ed3\u679c\u8868\u793a\uff0c\u53ea\u6709\u4e00\u4e2a\u81ea\u5b9a\u4e49\u63a5\u53e3\uff0c\u63a5\u53e3\u540d\u5c31\u662f\u65b9\u6cd5\u540d\uff0c\u800c\u4e14\u67e5\u8be2\u7ed3\u679c\u8fd8\u7ed9\u51fa\u4e86\u63a5\u53e3\u8c03\u7528\u7684\u793a\u4f8b\u3002\u6211\u4eec\u6765\u5c1d\u8bd5\u8c03\u7528\u4e00\u4e0b\u81ea\u5df1\u5b9a\u4e49\u7684\u67e5\u8be2\u63a5\u53e3\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042233981\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\uff0c\u5728 BookRepository \u4e2d\u5b9a\u4e49\u4efb\u610f\u591a\u4e2a\u67e5\u8be2\u65b9\u6cd5\uff0c\u67e5\u8be2\u65b9\u6cd5\u7684\u5b9a\u4e49\u89c4\u5219\u548c Jpa \u4e2d\u4e00\u6a21\u4e00\u6837\uff08\u4e0d\u61c2 Jpa \u7684\u5c0f\u4f19\u4f34\uff0c\u53ef\u4ee5\u53c2\u8003<a href=\"https:\/\/link.segmentfault.com\/?enc=cErOMQSPSYpi8kvy1wWp1A%3D%3D.FKiGAdc7zc6nruTsHz0qGzP2DZPgFyaIi0Yz247jzlbRA2E3YTeswMal%2BRrz1ESWsJtfSNs3qvyuq9Fl8Wn%2FGQ%3D%3D\" rel=\"nofollow\">\u5e72\u8d27|\u4e00\u6587\u8bfb\u61c2 Spring Data Jpa\uff01<\/a>\uff0c\u6216\u8005\u5728\u677e\u54e5\u4e2a\u4eba\u7f51\u7ad9 www.javaboy.org \u4e0a\u641c\u7d22 JPA\uff0c\u6709\u76f8\u5173\u6559\u7a0b\u53c2\u8003\uff09\u3002\u4f46\u662f\uff0c\u8fd9\u6837\u6709\u4e00\u4e2a\u7f3a\u9677\uff0c\u5c31\u662f Jpa \u4e2d\u65b9\u6cd5\u540d\u592a\u957f\uff0c\u56e0\u6b64\uff0c\u5982\u679c\u4e0d\u60f3\u4f7f\u7528\u65b9\u6cd5\u540d\u4f5c\u4e3a\u63a5\u53e3\u540d\uff0c\u5219\u53ef\u4ee5\u81ea\u5b9a\u4e49\u63a5\u53e3\u540d\uff1a<\/p>\n<pre><code class=\"java\">public interface BookRepository extends JpaRepository&lt;Book, Long&gt; {\n    @RestResource(rel = &quot;byauthor&quot;,path = &quot;byauthor&quot;)\n    List&lt;Book&gt; findBookByAuthorContaining(@Param(&quot;author&quot;) String author);\n}<\/code><\/pre>\n<p>@RestResource \u6ce8\u89e3\u4e2d\uff0c\u4e24\u4e2a\u53c2\u6570\u7684\u542b\u4e49\uff1a<\/p>\n<ul>\n<li>rel \u8868\u793a\u63a5\u53e3\u67e5\u8be2\u4e2d\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u7684 key<\/li>\n<li>\n<p>path \u8868\u793a\u8bf7\u6c42\u8def\u5f84<\/p>\n<\/li>\n<\/ul>\n<p>\u8fd9\u6837\u5b9a\u4e49\u5b8c\u6210\u540e\uff0c\u8868\u793a\u63a5\u53e3\u540d\u4e3a byauthor \uff0c\u91cd\u542f\u9879\u76ee\uff0c\u7ee7\u7eed\u67e5\u8be2\u63a5\u53e3\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042233982\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u9664\u4e86 <code>rel<\/code> \u548c <code>path<\/code> \u4e24\u4e2a\u5c5e\u6027\u4e4b\u5916\uff0c<code>@RestResource<\/code> \u4e2d\u8fd8\u6709\u4e00\u4e2a\u5c5e\u6027\uff0c<code>exported<\/code> \u8868\u793a\u662f\u5426\u66b4\u9732\u63a5\u53e3\uff0c\u9ed8\u8ba4\u4e3a <code>true<\/code> \uff0c\u8868\u793a\u66b4\u9732\u63a5\u53e3\uff0c\u5373\u65b9\u6cd5\u53ef\u4ee5\u5728\u524d\u7aef\u8c03\u7528\uff0c\u5982\u679c\u4ec5\u4ec5\u53ea\u662f\u60f3\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\uff0c\u4e0d\u9700\u8981\u5728\u524d\u7aef\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8bbe\u7f6e <code>exported<\/code> \u5c5e\u6027\u4e3a <code>false<\/code> \u3002<\/p>\n<p>\u5982\u679c\u4e0d\u60f3\u66b4\u9732\u5b98\u65b9\u5b9a\u4e49\u597d\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\u6839\u636e <code>id<\/code> \u5220\u9664\u6570\u636e\uff0c\u53ea\u9700\u8981\u5728\u81ea\u5b9a\u4e49\u63a5\u53e3\u4e2d\u91cd\u5199\u8be5\u65b9\u6cd5\uff0c\u7136\u540e\u5728\u8be5\u65b9\u6cd5\u4e0a\u52a0 <code>@RestResource<\/code> \u6ce8\u89e3\u5e76\u4e14\u914d\u7f6e\u76f8\u5173\u5c5e\u6027\u5373\u53ef\u3002<\/p>\n<pre><code class=\"java\">public interface BookRepository extends JpaRepository&lt;Book, Long&gt; {\n    @RestResource(rel = &quot;byauthor&quot;,path = &quot;byauthor&quot;)\n    List&lt;Book&gt; findBookByAuthorContaining(@Param(&quot;author&quot;) String author);\n    @Override\n    @RestResource(exported = false)\n    void deleteById(Long aLong);\n}<\/code><\/pre>\n<p>\u53e6\u5916\u751f\u6210\u7684 JSON \u5b57\u7b26\u4e32\u4e2d\u7684\u96c6\u5408\u540d\u548c\u5355\u4e2a <code>item<\/code> \u7684\u540d\u5b57\u90fd\u662f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u7684\uff1a<\/p>\n<pre><code class=\"java\">@RepositoryRestResource(collectionResourceRel = &quot;bs&quot;,itemResourceRel = &quot;b&quot;,path = &quot;bs&quot;)\npublic interface BookRepository extends JpaRepository&lt;Book, Long&gt; {\n    @RestResource(rel = &quot;byauthor&quot;,path = &quot;byauthor&quot;)\n    List&lt;Book&gt; findBookByAuthorContaining(@Param(&quot;author&quot;) String author);\n    @Override\n    @RestResource(exported = false)\n    void deleteById(Long aLong);\n}<\/code><\/pre>\n<p><code>path<\/code> \u5c5e\u6027\u8868\u793a\u8bf7\u6c42\u8def\u5f84\uff0c\u8bf7\u6c42\u8def\u5f84\u9ed8\u8ba4\u662f\u7c7b\u540d\u9996\u5b57\u6bcd\u5c0f\u5199+s\uff0c\u53ef\u4ee5\u5728\u8fd9\u91cc\u81ea\u5df1\u91cd\u65b0\u5b9a\u4e49\u3002<\/p>\n<h4>\u5176\u4ed6\u914d\u7f6e<\/h4>\n<p>\u6700\u540e\uff0c\u4e5f\u53ef\u4ee5\u5728 application.properties \u4e2d\u914d\u7f6e REST \u57fa\u672c\u53c2\u6570\uff1a<\/p>\n<pre><code class=\"properties\">spring.data.rest.base-path=\/api\nspring.data.rest.sort-param-name=sort\nspring.data.rest.page-param-name=page\nspring.data.rest.limit-param-name=size\nspring.data.rest.max-page-size=20\nspring.data.rest.default-page-size=0\nspring.data.rest.return-body-on-update=true\nspring.data.rest.return-body-on-create=true<\/code><\/pre>\n<p>\u914d\u7f6e\u542b\u4e49\uff0c\u4ece\u4e0a\u5f80\u4e0b\uff0c\u4f9d\u6b21\u662f\uff1a<\/p>\n<ol>\n<li>\u7ed9\u6240\u6709\u7684\u63a5\u53e3\u6dfb\u52a0\u7edf\u4e00\u7684\u524d\u7f00<\/li>\n<li>\u914d\u7f6e\u6392\u5e8f\u53c2\u6570\u7684 key \uff0c\u9ed8\u8ba4\u662f sort<\/li>\n<li>\u914d\u7f6e\u5206\u9875\u67e5\u8be2\u65f6\u9875\u7801\u7684 key\uff0c\u9ed8\u8ba4\u662f page<\/li>\n<li>\u914d\u7f6e\u5206\u9875\u67e5\u8be2\u65f6\u6bcf\u9875\u67e5\u8be2\u9875\u6570\u7684 key\uff0c\u9ed8\u8ba4\u662fsize<\/li>\n<li>\u914d\u7f6e\u6bcf\u9875\u6700\u5927\u67e5\u8be2\u8bb0\u5f55\u6570\uff0c\u9ed8\u8ba4\u662f 20 \u6761<\/li>\n<li>\u5206\u9875\u67e5\u8be2\u65f6\u9ed8\u8ba4\u7684\u9875\u7801<\/li>\n<li>\u66f4\u65b0\u6210\u529f\u65f6\u662f\u5426\u8fd4\u56de\u66f4\u65b0\u8bb0\u5f55<\/li>\n<li>\u6dfb\u52a0\u6210\u529f\u65f6\u662f\u5426\u8fd4\u56de\u6dfb\u52a0\u8bb0\u5f55<\/li>\n<\/ol>\n<p>\u8fd9\u662f Spring Data Rest \u7684\u4e00\u4e2a\u7b80\u5355\u7528\u6cd5\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u770b\u5982\u4f55\u7ed9\u8fd9\u4e2a\u751f\u6210\u7684\u6587\u6863\u3002<\/p>\n<h3>4.2 \u751f\u6210\u63a5\u53e3\u6587\u6863<\/h3>\n<p>\u5bf9\u4e8e\u8fd9\u79cd\u4f60\u90fd\u6ca1\u770b\u5230\u63a5\u53e3\u7684\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6dfb\u52a0\u5982\u4e0b\u4f9d\u8d56\uff0c\u5c31\u53ef\u4ee5\u81ea\u52a8\u751f\u6210 API \u6587\u6863\u4e86\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springdoc&lt;\/groupId&gt;\n    &lt;artifactId&gt;springdoc-openapi-data-rest&lt;\/artifactId&gt;\n    &lt;version&gt;1.6.9&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springdoc&lt;\/groupId&gt;\n    &lt;artifactId&gt;springdoc-openapi-ui&lt;\/artifactId&gt;\n    &lt;version&gt;1.6.9&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u751f\u6210\u7684\u63a5\u53e3\u6587\u6863\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042233983\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h2>5. \u7ed3\u5408 Actuator<\/h2>\n<p>\u5728\u4e4b\u524d\u7684 Spring Boot \u6559\u7a0b\u4e2d\uff0c\u677e\u54e5\u8fd8\u548c\u5927\u5bb6\u4ecb\u7ecd\u8fc7 Spring Boot \u4e2d\u7684 actuator\uff0c\u8fd9\u4e2a\u5de5\u5177\u53ef\u4ee5\u81ea\u884c\u751f\u6210\u9879\u76ee\u8fd0\u884c\u6570\u636e\u7684\u7aef\u70b9\uff08endpoints\uff09\uff0c\u5982\u679c\u60f3\u628a\u8fd9\u4e9b\u7aef\u70b9\u4e5f\u7eb3\u5165\u5230 SpringDoc \u4e2d\u6765\uff0c\u90a3\u4e48\u53ea\u9700\u8981\u6dfb\u52a0\u5982\u4e0b\u914d\u7f6e\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"properties\">springdoc.show-actuator=true<\/code><\/pre>\n<p>\u81f3\u4e8e SpringDoc \u4f1a\u663e\u793a\u591a\u5c11\u4e2a Actuator \u7aef\u70b9\u51fa\u6765\uff0c\u90a3\u5c31\u8981\u770b Actuator \u66b4\u9732\u51fa\u6765\u591a\u5c11\u7aef\u70b9\u4e86\uff0c\u6700\u7ec8\u663e\u793a\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042233984\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4e0d\u8fc7\u8fd9\u91cc\u8fd8\u6709\u4e00\u4e2a\u73a9\u6cd5\uff01<\/p>\n<p>SpringDoc \u626e\u6f14\u7684\u89d2\u8272\u6bd5\u7adf\u4e0d\u662f\u4e1a\u52a1\u529f\u80fd\uff0c\u800c\u662f\u9879\u76ee\u7684\u8f85\u52a9\u529f\u80fd\uff0c\u6240\u4ee5\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e4b\u4ece\u4e1a\u52a1\u4e2d\u5265\u79bb\uff0c\u653e\u5230 Actuator \u4e2d\uff0c\u6bd5\u7adf Actuator \u4e13\u5e72\u8fd9\u79cd\u4e8b\u3002\u90a3\u4e48\u53ea\u9700\u8981\u589e\u52a0\u5982\u4e0b\u4e24\u4e2a\u914d\u7f6e\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"properties\">springdoc.use-management-port=true\nmanagement.endpoints.web.exposure.include=openapi, swagger-ui\nmanagement.server.port=9090<\/code><\/pre>\n<p>\u914d\u7f6e\u5b8c\u6210\u540e\uff0c\u5c06\u6765\u5c31\u53ef\u4ee5\u5728 Actuator \u4e2d\u53bb\u67e5\u770b\u63a5\u53e3\u6587\u6863\u548c\u5bf9\u5e94\u7684\u9875\u9762\u4e86\uff0c\u8bbf\u95ee\u5730\u5740\u662f\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=SWw7SShvVbSm1kD%2FZDvQfQ%3D%3D.RYSB7L4KN0KQn2TeWTc8z6OA48sTISfpM3o4NvDeZqzovG6KDO610goecYYURsdhHNULk%2Fnh3oF2j1z05FwJ0A%3D%3D\" rel=\"nofollow\">http:\/\/localhost:9090\/actuator\/swagger-ui\/index.html<\/a><\/li>\n<\/ul>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042233985\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h2>6. \u5207\u6362\u5230 Swagger<\/h2>\n<p>\u5982\u679c\u4f60\u5728\u9879\u76ee\u4e2d\u5df2\u7ecf\u4f7f\u7528\u4e86 Swagger \u4e86\uff0c\u90a3\u4e48\u4e5f\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u7684\u5207\u6362\u5230 SpringDoc \u4e0a\u9762\u6765\uff0c\u5207\u6362\u7684\u65f6\u5019\uff0c\u9996\u5148\u5f15\u5165 SpringDoc \u4f9d\u8d56\uff1a<\/p>\n<pre><code class=\"xml\">&lt;dependency&gt;\n   &lt;groupId&gt;org.springdoc&lt;\/groupId&gt;\n   &lt;artifactId&gt;springdoc-openapi-ui&lt;\/artifactId&gt;\n   &lt;version&gt;1.6.9&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>Swagger \u548c SpringDoc \u6ce8\u89e3\u7684\u5bf9\u5e94\u5173\u7cfb\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>@Api \u2192 @Tag<\/li>\n<li>@ApiIgnore \u2192 @Parameter(hidden = true) or @Operation(hidden = true) or @Hidden<\/li>\n<li>@ApiImplicitParam \u2192 @Parameter<\/li>\n<li>@ApiImplicitParams \u2192 @Parameters<\/li>\n<li>@ApiModel \u2192 @Schema<\/li>\n<li>@ApiModelProperty(hidden = true) \u2192 @Schema(accessMode = READ_ONLY)<\/li>\n<li>@ApiModelProperty \u2192 @Schema<\/li>\n<li>@ApiOperation(value = &#8220;foo&#8221;, notes = &#8220;bar&#8221;) \u2192 @Operation(summary = &#8220;foo&#8221;, description = &#8220;bar&#8221;)<\/li>\n<li>@ApiParam \u2192 @Parameter<\/li>\n<li>@ApiResponse(code = 404, message = &#8220;foo&#8221;) \u2192 @ApiResponse(responseCode = &#8220;404&#8221;, description = &#8220;foo&#8221;)<\/li>\n<\/ul>\n<p>\u4ee5\u524d\u6211\u4eec\u5728 Swagger \u4e2d\u914d\u7f6e\u63a5\u53e3\u626b\u63cf\u7684\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">@Bean\npublic Docket publicApi() {\n    return new Docket(DocumentationType.SWAGGER_2)\n            .select()\n            .apis(RequestHandlerSelectors.basePackage(&quot;org.github.springshop.web.public&quot;))\n            .paths(PathSelectors.regex(&quot;\/public.*&quot;))\n            .build()\n            .groupName(&quot;springshop-public&quot;)\n            .apiInfo(apiInfo());\n}\n\n@Bean\npublic Docket adminApi() {\n    return new Docket(DocumentationType.SWAGGER_2)\n            .select()\n            .apis(RequestHandlerSelectors.basePackage(&quot;org.github.springshop.web.admin&quot;))\n            .paths(PathSelectors.regex(&quot;\/admin.*&quot;))\n            .apis(RequestHandlerSelectors.withMethodAnnotation(Admin.class))\n            .build()\n            .groupName(&quot;springshop-admin&quot;)\n            .apiInfo(apiInfo());\n}<\/code><\/pre>\n<p>\u73b0\u5728\u5728 SpringDoc \u4e2d\u5219\u6309\u7167\u5982\u4e0b\u65b9\u5f0f\u8fdb\u884c\u914d\u7f6e\u5373\u53ef\uff08\u8fd8\u53ef\u4ee5\u6309\u7167\u6ce8\u89e3\u53bb\u6807\u8bb0\u9700\u8981\u751f\u6210\u63a5\u53e3\u6587\u6863\u7684\u65b9\u6cd5\uff09\uff1a<\/p>\n<pre><code class=\"java\">@Configuration\npublic class SpringDocConfig {\n    @Bean\n    public GroupedOpenApi publicApi() {\n        return GroupedOpenApi.builder()\n                .group(&quot;springshop-public&quot;)\n                .pathsToMatch(&quot;\/public\/**&quot;)\n                .build();\n    }\n    @Bean\n    public GroupedOpenApi adminApi() {\n        return GroupedOpenApi.builder()\n                .group(&quot;springshop-admin&quot;)\n                .pathsToMatch(&quot;\/admin\/**&quot;)\n                .addOpenApiMethodFilter(method -&gt; method.isAnnotationPresent(RequestMapping.class))\n                .build();\n    }\n}<\/code><\/pre>\n<p>\u5f53\u7136\uff0c\u5982\u679c\u4f60\u5e76\u4e0d\u9700\u8981\u5bf9\u63a5\u53e3\u6587\u6863\u8fdb\u884c\u5206\u7ec4\uff0c\u90a3\u4e48\u4e5f\u53ef\u4ee5\u4e0d\u4f7f\u7528 Java \u914d\u7f6e\uff0c\u76f4\u63a5\u5728 application.properties \u4e2d\u8fdb\u884c\u914d\u7f6e\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"properties\">springdoc.packages-to-scan=org.javaboy.spring_doc.controller\nspringdoc.paths-to-match=\/**<\/code><\/pre>\n<p>\u5728 SpringDoc \u4e2d\uff0c\u5982\u679c\u4f60\u60f3\u914d\u7f6e Swagger UI\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u65b9\u5f0f\u8fdb\u884c\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"java\">@Bean\nOpenAPI springShopOpenAPI() {\n    return new OpenAPI()\n            .info(new Info().title(&quot;\u6c5f\u5357\u4e00\u70b9\u96e8&quot;)\n                    .description(&quot;Spring Boot \u6559\u7a0b&quot;)\n                    .version(&quot;v0.0.1&quot;)\n                    .license(new License().name(&quot;Apache 2.0&quot;).url(&quot;http:\/\/www.javaboy.org&quot;)))\n            .externalDocs(new ExternalDocumentation()\n                    .description(&quot;\u4e00\u4e9b\u63cf\u8ff0\u4fe1\u606f&quot;)\n                    .url(&quot;https:\/\/github.com\/lenve\/vhr&quot;));\n}<\/code><\/pre>\n<p>\u597d\u5566\uff0c\u5e38\u89c1\u7528\u6cd5\u5927\u6982\u5c31\u662f\u8fd9\u6837\uff0c\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u53bb\u8bd5\u8bd5\u54e6~\u5173\u4e8e SpringDoc \u7684\u66f4\u591a\u73a9\u6cd5\uff0c\u5927\u5bb6\u4e5f\u53ef\u4ee5\u53c2\u8003\u5b98\u65b9\u6587\u6863\uff1aspringdoc.org\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042233968\">https:\/\/segmentfault.com\/a\/1190000042233968<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>@[toc]\u4e0a\u5468\u677e\u54e5\u5199\u4e86\u4e00\u7bc7\u6587\u7ae0\u548c\u5c0f\u4f19\u4f34\u4eec\u5206\u4eab Swagger3 \u5728 Spring Boot \u4e2d\u7684\u7528\u6cd5\uff0c\u8bc4\u8bba\u4e2d [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006045","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006045","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006045"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006045\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}