{"id":4197,"date":"2021-12-02T11:28:50","date_gmt":"2021-12-02T03:28:50","guid":{"rendered":"http:\/\/123.57.164.21\/?p=4197"},"modified":"2021-12-02T11:41:10","modified_gmt":"2021-12-02T03:41:10","slug":"%e6%9e%84%e5%bb%ba%e5%be%ae%e6%9c%8d%e5%8a%a1%ef%bc%9aspring-boot-%e5%85%a5%e9%97%a8%e7%af%87","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=4197","title":{"rendered":"\u6784\u5efa\u5fae\u670d\u52a1\uff1aSpring boot \u5165\u95e8\u7bc7"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><strong>\u4ec0\u4e48\u662f<\/strong>Spring Boot<\/h4>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Spring Boot \u662f\u7531 Pivotal \u56e2\u961f\u63d0\u4f9b\u7684\u5168\u65b0\u6846\u67b6\uff0c\u5176\u8bbe\u8ba1\u76ee\u7684\u662f\u7528\u6765\u7b80\u5316\u65b0 Spring \u5e94\u7528\u7684\u521d\u59cb\u642d\u5efa\u4ee5\u53ca\u5f00\u53d1\u8fc7\u7a0b\u3002\u8be5\u6846\u67b6\u4f7f\u7528\u4e86\u7279\u5b9a\u7684\u65b9\u5f0f\u6765\u8fdb\u884c\u914d\u7f6e\uff0c\u4ece\u800c\u4f7f\u5f00\u53d1\u4eba\u5458\u4e0d\u518d\u9700\u8981\u5b9a\u4e49\u6837\u677f\u5316\u7684\u914d\u7f6e\u3002\u7528\u6211\u7684\u8bdd\u6765\u7406\u89e3\uff0c\u5c31\u662f Spring Boot \u5176\u5b9e\u4e0d\u662f\u4ec0\u4e48\u65b0\u7684\u6846\u67b6\uff0c\u5b83\u9ed8\u8ba4\u914d\u7f6e\u4e86\u5f88\u591a\u6846\u67b6\u7684\u4f7f\u7528\u65b9\u5f0f\uff0c\u5c31\u50cf Maven \u6574\u5408\u4e86\u6240\u6709\u7684 Jar \u5305\uff0cSpring Boot \u6574\u5408\u4e86\u6240\u6709\u7684\u6846\u67b6\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u4f7f\u7528&nbsp;<\/strong>Spring Boot<strong>\u6709\u4ec0\u4e48\u597d\u5904<\/strong><\/h4>\n\n\n\n<p>\u5176\u5b9e\u5c31\u662f\u7b80\u5355\u3001\u5feb\u901f\u3001\u65b9\u4fbf\uff01\u5e73\u65f6\u5982\u679c\u6211\u4eec\u9700\u8981\u642d\u5efa\u4e00\u4e2a Spring Web \u9879\u76ee\u7684\u65f6\u5019\u9700\u8981\u600e\u4e48\u505a\u5462\uff1f<\/p>\n\n\n\n<ul><li>1\uff09\u914d\u7f6e web.xml\uff0c\u52a0\u8f7d Spring \u548c Spring mvc<\/li><li>2\uff09\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u3001\u914d\u7f6e Spring \u4e8b\u52a1<\/li><li>3\uff09\u914d\u7f6e\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u7684\u8bfb\u53d6\uff0c\u5f00\u542f\u6ce8\u89e3<\/li><li>4\uff09\u914d\u7f6e\u65e5\u5fd7\u6587\u4ef6<\/li><li>&#8230;<\/li><li>\u914d\u7f6e\u5b8c\u6210\u4e4b\u540e\u90e8\u7f72 Tomcat \u8c03\u8bd5<\/li><li>&#8230;<\/li><\/ul>\n\n\n\n<p>\u73b0\u5728\u975e\u5e38\u6d41\u884c\u5fae\u670d\u52a1\uff0c\u5982\u679c\u6211\u8fd9\u4e2a\u9879\u76ee\u4ec5\u4ec5\u53ea\u662f\u9700\u8981\u53d1\u9001\u4e00\u4e2a\u90ae\u4ef6\uff0c\u5982\u679c\u6211\u7684\u9879\u76ee\u4ec5\u4ec5\u662f\u751f\u4ea7\u4e00\u4e2a\u79ef\u5206\uff1b\u6211\u90fd\u9700\u8981\u8fd9\u6837\u6298\u817e\u4e00\u904d!<\/p>\n\n\n\n<p>\u4f46\u662f\u5982\u679c\u4f7f\u7528 Spring Boot \u5462\uff1f<br>\u5f88\u7b80\u5355\uff0c\u6211\u4ec5\u4ec5\u53ea\u9700\u8981\u975e\u5e38\u5c11\u7684\u51e0\u4e2a\u914d\u7f6e\u5c31\u53ef\u4ee5\u8fc5\u901f\u65b9\u4fbf\u7684\u642d\u5efa\u8d77\u6765\u4e00\u5957 Web \u9879\u76ee\u6216\u8005\u662f\u6784\u5efa\u4e00\u4e2a\u5fae\u670d\u52a1\uff01<\/p>\n\n\n\n<p>\u4f7f\u7528 Spring Boot \u5230\u5e95\u6709\u591a\u723d\uff0c\u7528\u4e0b\u9762\u8fd9\u5e45\u56fe\u6765\u8868\u8fbe<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-2.png\" alt=\"\" class=\"wp-image-4200\" width=\"189\" height=\"179\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-2.png 364w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-2-300x284.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-2-230x217.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-2-350x331.png 350w\" sizes=\"(max-width: 189px) 100vw, 189px\" \/><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5feb\u901f\u5165\u95e8<\/strong><\/h4>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u8bf4\u4e86\u90a3\u4e48\u591a\uff0c\u624b\u75d2\u75d2\u7684\u5f88\uff0c\u9a6c\u4e0a\u6765\u4e00\u53d1\u8bd5\u8bd5!<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Maven \u6784\u5efa\u9879\u76ee<\/strong><\/h5>\n\n\n\n<ul><li>1\u3001\u8bbf\u95ee http:\/\/start.spring.io\/<\/li><li>2\u3001\u9009\u62e9\u6784\u5efa\u5de5\u5177 Maven Project\u3001Java\u3001Spring Boot \u7248\u672c 2.1.3 \u4ee5\u53ca\u4e00\u4e9b\u5de5\u7a0b\u57fa\u672c\u4fe1\u606f<\/li><\/ul>\n\n\n\n<ul><li>3\u3001\u70b9\u51fb Generate Project \u4e0b\u8f7d\u9879\u76ee\u538b\u7f29\u5305<\/li><li>4\u3001\u89e3\u538b\u540e\uff0c\u4f7f\u7528 Idea \u5bfc\u5165\u9879\u76ee\uff0cFile -&gt; New -&gt; Model from Existing Source.. -&gt; \u9009\u62e9\u89e3\u538b\u540e\u7684\u6587\u4ef6\u5939 -&gt; OK\uff0c\u9009\u62e9 Maven \u4e00\u8def Next\uff0cOK done!<\/li><li>5\u3001\u5982\u679c\u4f7f\u7528\u7684\u662f Eclipse\uff0cImport -&gt; Existing Maven Projects -&gt; Next -&gt; \u9009\u62e9\u89e3\u538b\u540e\u7684\u6587\u4ef6\u5939 -&gt; Finsh\uff0cOK done!<\/li><\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u9879\u76ee\u7ed3\u6784\u4ecb\u7ecd<\/strong><\/h5>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-3-1024x530.png\" alt=\"\" class=\"wp-image-4203\" width=\"685\" height=\"354\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-3-1024x530.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-3-300x155.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-3-768x397.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-3-1536x795.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-3-830x429.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-3-230x119.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-3-350x181.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-3-480x248.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-3.png 1616w\" sizes=\"(max-width: 685px) 100vw, 685px\" \/><\/figure><\/div>\n\n\n\n<p>\u5982\u4e0a\u56fe\u6240\u793a\uff0cSpring Boot\u7684\u57fa\u7840\u7ed3\u6784\u5171\u4e09\u4e2a\u6587\u4ef6:<\/p>\n\n\n\n<ul><li>src\/main\/java&nbsp; \u7a0b\u5e8f\u5f00\u53d1\u4ee5\u53ca\u4e3b\u7a0b\u5e8f\u5165\u53e3<\/li><li>src\/main\/resources \u914d\u7f6e\u6587\u4ef6<\/li><li>src\/test\/java&nbsp; \u6d4b\u8bd5\u7a0b\u5e8f<\/li><\/ul>\n\n\n\n<p>\u53e6\u5916\uff0cspingboot\u5efa\u8bae\u7684\u76ee\u5f55\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>root package\u7ed3\u6784\uff1acom.example.myproject<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-4-1024x465.png\" alt=\"\" class=\"wp-image-4204\" width=\"639\" height=\"290\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-4-1024x465.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-4-300x136.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-4-768x349.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-4-1536x697.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-4-830x377.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-4-230x104.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-4-350x159.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-4-480x218.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-4.png 1758w\" sizes=\"(max-width: 639px) 100vw, 639px\" \/><\/figure><\/div>\n\n\n\n<ol type=\"1\"><li>Application.java&nbsp;\u5efa\u8bae\u653e\u5230\u8ddf\u76ee\u5f55\u4e0b\u9762,\u4e3b\u8981\u7528\u4e8e\u505a\u4e00\u4e9b\u6846\u67b6\u914d\u7f6e<\/li><li>domain\u76ee\u5f55\u4e3b\u8981\u7528\u4e8e\u5b9e\u4f53\uff08Entity\uff09\u4e0e\u6570\u636e\u8bbf\u95ee\u5c42\uff08Repository\uff09<\/li><li>service&nbsp;\u5c42\u4e3b\u8981\u662f\u4e1a\u52a1\u7c7b\u4ee3\u7801<\/li><li>controller \u8d1f\u8d23\u9875\u9762\u8bbf\u95ee\u63a7\u5236<\/li><\/ol>\n\n\n\n<p>\u91c7\u7528\u9ed8\u8ba4\u914d\u7f6e\u53ef\u4ee5\u7701\u53bb\u5f88\u591a\u914d\u7f6e\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u559c\u6b22\u6765\u8fdb\u884c\u66f4\u6539<\/p>\n\n\n\n<p>\u6700\u540e\uff0c\u542f\u52a8Application main\u65b9\u6cd5\uff0c\u81f3\u6b64\u4e00\u4e2ajava\u9879\u76ee\u642d\u5efa\u597d\u4e86\uff01<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u5f15\u5165 Web\u00a0\u6a21\u5757<\/strong><\/h5>\n\n\n\n<p><strong>1\u3001pom.xml\u4e2d\u6dfb\u52a0\u652f\u6301web\u7684\u6a21\u5757\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\n\n&lt;dependency>\n        &lt;groupId>org.springframework.boot&lt;\/groupId>\n        &lt;artifactId>spring-boot-starter-web&lt;\/artifactId>\n &lt;\/dependency>\n\n<\/pre>\n\n\n\n<p>pom.xml\u6587\u4ef6\u4e2d\u9ed8\u8ba4\u6709\u4e24\u4e2a\u6a21\u5757\uff1a<\/p>\n\n\n\n<p>spring-boot-starter\uff1a\u6838\u5fc3\u6a21\u5757\uff0c\u5305\u62ec\u81ea\u52a8\u914d\u7f6e\u652f\u6301\u3001\u65e5\u5fd7\u548cYAML\uff1b<\/p>\n\n\n\n<p>spring-boot-starter-test\uff1a\u6d4b\u8bd5\u6a21\u5757\uff0c\u5305\u62ecJUnit\u3001Hamcrest\u3001Mockito\u3002<\/p>\n\n\n\n<p><strong>\u7f16\u5199controller\u5185\u5bb9<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@RestController\npublic class HelloWorldController {\n    @RequestMapping(\"\/hello\")\n    public String index() {\n        return \"Hello World\";\n    }\n}<\/pre>\n\n\n\n<p>@RestController\u7684\u610f\u601d\u5c31\u662fcontroller\u91cc\u9762\u7684\u65b9\u6cd5\u90fd\u4ee5json\u683c\u5f0f\u8f93\u51fa\uff0c\u4e0d\u7528\u518d\u5199\u4ec0\u4e48jackjson\u914d\u7f6e\u7684\u4e86\uff01<\/p>\n\n\n\n<p>3.\u542f\u52a8\u4e3b\u7a0b\u5e8f\uff0c\u6253\u5f00\u6d4f\u89c8\u5668\u8bbf\u95eehttp:\/\/localhost:8080\/hello\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u6548\u679c\u4e86\uff0c\u6709\u6728\u6709\u5f88\u7b80\u5355\uff01<\/p>\n\n\n\n<p><strong>\u5982\u4f55\u505a\u5355\u5143\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<p>\u6253\u5f00\u7684src\/test\/\u4e0b\u7684\u6d4b\u8bd5\u5165\u53e3\uff0c\u7f16\u5199\u7b80\u5355\u7684http\u8bf7\u6c42\u6765\u6d4b\u8bd5\uff1b\u4f7f\u7528mockmvc\u8fdb\u884c\uff0c\u5229\u7528MockMvcResultHandlers.print()\u6253\u5370\u51fa\u6267\u884c\u7ed3\u679c\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@RunWith(SpringRunner.class)\n  @SpringBootTest\n\npublic class HelloWorldControlerTests {\n    private MockMvc mvc;\n    @Before\n    public void setUp() throws Exception {\n        mvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build();\n    }\n    @Test\n    public void getHello() throws Exception {\n    mvc.perform(MockMvcRequestBuilders.get(\"\/hello\").accept(MediaType.APPLICATION_JSON))\n                .andExpect(MockMvcResultMatchers.status().isOk())\n                .andDo(MockMvcResultHandlers.print())\n                .andReturn();\n    }\n}<\/pre>\n\n\n\n<p><strong>\u5f00\u53d1\u73af\u5883\u7684\u8c03\u8bd5<\/strong><\/p>\n\n\n\n<p>\u70ed\u542f\u52a8\u5728\u6b63\u5e38\u5f00\u53d1\u9879\u76ee\u4e2d\u5df2\u7ecf\u5f88\u5e38\u89c1\u4e86\u5427\uff0c\u867d\u7136\u5e73\u65f6\u5f00\u53d1 web \u9879\u76ee\u8fc7\u7a0b\u4e2d\uff0c\u6539\u52a8\u9879\u76ee\u542f\u91cd\u542f\u603b\u662f\u62a5\u9519\uff1b\u4f46 Spring Boot \u5bf9\u8c03\u8bd5\u652f\u6301\u5f88\u597d\uff0c\u4fee\u6539\u4e4b\u540e\u53ef\u4ee5\u5b9e\u65f6\u751f\u6548\uff0c\u9700\u8981\u6dfb\u52a0\u4ee5\u4e0b\u7684\u914d\u7f6e\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;dependencies>\n\n    &lt;dependency>\n\n        &lt;groupId>org.springframework.boot&lt;\/groupId>\n\n        &lt;artifactId>spring-boot-devtools&lt;\/artifactId>\n\n        &lt;optional>true&lt;\/optional>\n\n   &lt;\/dependency>\n\n&lt;\/dependencies>\n\n&lt;build>\n\n    &lt;plugins>\n\n        &lt;plugin>\n\n            &lt;groupId>org.springframework.boot&lt;\/groupId>\n\n            &lt;artifactId>spring-boot-maven-plugin&lt;\/artifactId>\n\n            &lt;configuration>\n\n                &lt;fork>true&lt;\/fork>\n\n            &lt;\/configuration>\n\n        &lt;\/plugin>\n\n   &lt;\/plugins>\n\n&lt;\/build><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h4>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4f7f\u7528 Spring Boot \u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u3001\u5feb\u901f\u642d\u5efa\u9879\u76ee\uff0c\u4f7f\u6211\u4eec\u4e0d\u7528\u5173\u5fc3\u6846\u67b6\u4e4b\u95f4\u7684\u517c\u5bb9\u6027\uff0c\u9002\u7528\u7248\u672c\u7b49\u5404\u79cd\u95ee\u9898\uff0c\u6211\u4eec\u60f3\u4f7f\u7528\u4efb\u4f55\u4e1c\u897f\uff0c\u4ec5\u4ec5\u6dfb\u52a0\u4e00\u4e2a\u914d\u7f6e\u5c31\u53ef\u4ee5\uff0c\u6240\u4ee5\u4f7f\u7528 Spring Boot \u975e\u5e38\u9002\u5408\u6784\u5efa\u5fae\u670d\u52a1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662fSpring Boot Spring Boot \u662f\u7531 Pivotal \u56e2\u961f\u63d0\u4f9b\u7684\u5168\u65b0\u6846\u67b6\uff0c\u5176\u8bbe\u8ba1\u76ee\u7684\u662f\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4197"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4197"}],"version-history":[{"count":11,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4197\/revisions"}],"predecessor-version":[{"id":4211,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4197\/revisions\/4211"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}