{"id":883,"date":"2021-02-23T10:16:00","date_gmt":"2021-02-23T02:16:00","guid":{"rendered":"http:\/\/www.langmanezhuang.com\/index.php\/2021\/02\/23\/springboot%e5%9f%ba%e7%a1%80%e6%a0%b8%e5%bf%83\/"},"modified":"2021-02-23T10:16:00","modified_gmt":"2021-02-23T02:16:00","slug":"springboot%e5%9f%ba%e7%a1%80%e6%a0%b8%e5%bf%83","status":"publish","type":"post","link":"http:\/\/blog.langmanezhuang.com\/index.php\/2021\/02\/23\/springboot%e5%9f%ba%e7%a1%80%e6%a0%b8%e5%bf%83\/","title":{"rendered":"Springboot\u57fa\u7840\u6838\u5fc3"},"content":{"rendered":"<p>1.Springboot\u4ecb\u7ecd<\/p>\n<p>&nbsp;Spring Boot\u662f\u7531Pivotal\u56e2\u961f\u63d0\u4f9b\u7684\u5168\u65b0\u6846\u67b6\uff0c\u5176\u8bbe\u8ba1\u76ee\u7684\u662f\u7528\u6765\u7b80\u5316\u65b0Spring\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\u662fspring 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\u50cfmaven\u6574\u5408\u4e86\u6240\u6709\u7684jar\u5305\uff0cspring boot\u6574\u5408\u4e86\u6240\u6709\u7684\u6846\u67b6<\/p>\n<p>2.Springboot\u9879\u76ee\u642d\u5efa<\/p>\n<p>\uff081\uff09.\u901a\u8fc7Eclipse\u8fdb\u884c\u6784\u5efa<\/p>\n<p>\uff082\uff09.\u901a\u8fc7Idea\u8fdb\u884c\u6784\u5efa<\/p>\n<p>\uff083\uff09.\u901a\u8fc7Spring\u7f51\u7ad9\u8fdb\u884c\u6784\u5efa\uff08\u63a8\u8350\uff09<\/p>\n<p>\u3000\u3000<\/p>\n<div class=\"cnblogs_Highlighter\">\n<pre class=\"brush:csharp;gutter:true;\">\u8bbf\u95eehttps:\/\/start.spring.io\/\n\u9009\u62e9\u6784\u5efa\u5de5\u5177Maven Project\u3001Spring Boot\u7248\u672c\n\u70b9\u51fbGenerate Project\u4e0b\u8f7d\u9879\u76ee\u538b\u7f29\u5305\n<\/pre>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.langmanezhuang.com\/blog\/20221126\/154-543925-20210223185141106-85758528.png\" alt=\"\" width=\"1019\" height=\"503\" loading=\"lazy\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;3.Springboot\u9879\u76ee\u7ed3\u6784<\/p>\n<p>&nbsp;<\/p>\n<div class=\"cnblogs_Highlighter\">\n<pre class=\"brush:csharp;gutter:true;\">src\/main\/java&nbsp; \u7a0b\u5e8f\u5f00\u53d1\u4ee5\u53ca\u4e3b\u7a0b\u5e8f\u5165\u53e3\nsrc\/main\/resources \u914d\u7f6e\u6587\u4ef6\nsrc\/test\/java&nbsp; \u6d4b\u8bd5\u7a0b\u5e8f\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>4.Springboot\u914d\u7f6e\u6587\u4ef6<\/p>\n<div class=\"cnblogs_Highlighter\">\n<pre class=\"brush:csharp;gutter:true;\">pom.xml\u6587\u4ef6\u4e2d\u9ed8\u8ba4\u6709\u4e24\u4e2a\u6a21\u5757\uff1a\n\nspring-boot-starter\uff1a\u6838\u5fc3\u6a21\u5757\uff0c\u5305\u62ec\u81ea\u52a8\u914d\u7f6e\u652f\u6301\u3001\u65e5\u5fd7\u548cYAML\uff1b\n\nspring-boot-starter-test\uff1a\u6d4b\u8bd5\u6a21\u5757\uff0c\u5305\u62ecJUnit\u3001Hamcrest\u3001Mockito\u3002<\/pre>\n<\/div>\n<p><span style=\"color: #000000;\">Web\u6a21\u5757\u5f15\u5165&nbsp;&nbsp;<\/span><\/p>\n<p><span style=\"color: #ff0000;\"><img decoding=\"async\" src=\"http:\/\/cdn.langmanezhuang.com\/blog\/20221126\/155-543925-20210223211116717-1468427765.png\" alt=\"\" loading=\"lazy\" \/><\/span><\/p>\n<div class=\"cnblogs_Highlighter\">\n<pre class=\"brush:bash;gutter:true;\">\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\n\t\t&lt;\/dependency&gt;\n<\/pre>\n<\/div>\n<p>\u3000<\/p>\n<p><span style=\"color: #0000ff;\"><a href=\"https:\/\/www.cnblogs.com\/JoePotter\/p\/14443222.html\" target=\"_blank\" rel=\"noopener\"><span style=\"color: #0000ff;\">&nbsp;Springboot\u9ed8\u8ba4\u914d\u7f6e\u6587\u4ef6application.properties\u7684\u5e38\u89c1\u914d\u7f6e\u5c5e\u6027<\/span><\/a><\/span><\/p>\n<p>&nbsp;<span style=\"color: #ff0000;\">\u4f7f\u7528spring 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\u7528sping boot\u975e\u5e38\u9002\u5408\u6784\u5efa\u5fae\u670d\u52a1\u3002<\/span><\/p>\n<p>5.Springboot\u5e38\u7528\u6ce8\u89e3<\/p>\n<ol>\n<li>@SpringBootApplication\u3000\n<p>\u5728\u7cfb\u7edf\u542f\u52a8\u7c7b\u91cc\u9762\uff0c\u90fd\u52a0\u5165\u4e86\u6b64\u542f\u52a8\u6ce8\u89e3\uff0c\u6b64\u6ce8\u89e3\u662f\u4e2a<span style=\"color: #ff0000;\">\u7ec4\u5408\u6ce8\u89e3<\/span>\uff0c\u5305\u62ec@SpringBootConfiguration\u3001@EnableAutoConfiguration\u548c@ComponentScan\u6ce8\u89e3\u3002<\/p>\n<p>@SpringBootConfiguration \u7ee7\u627f\u81f3@Configuration\uff0c\u5bf9\u4e8e\u719f\u6089spring\u7684\u5f00\u53d1\u8005\u800c\u8a00\uff0c\u6b64\u6807\u6ce8\u5f53\u524d\u7c7b\u662f\u914d\u7f6e\u7c7b\uff0c\u5e76\u4f1a\u5c06\u5f53\u524d\u7c7b\u5185\u58f0\u660e\u7684\u4e00\u4e2a\u6216\u591a\u4e2a\u4ee5@Bean\u6ce8\u89e3\u6807\u8bb0\u7684\u65b9\u6cd5\u7684\u5b9e\u4f8b\u7eb3\u5165\u5230srping\u5bb9\u5668\u4e2d\uff0c\u5e76\u4e14\u5b9e\u4f8b\u540d\u5c31\u662f\u65b9\u6cd5\u540d\u3002<\/p>\n<p>@EnableAutoConfiguration \u8fd9\u4e2a\u6ce8\u89e3\u5c31\u662f<span style=\"color: #ff0000;\">springboot\u80fd\u81ea\u52a8\u8fdb\u884c\u914d\u7f6e\u7684\u9b54\u6cd5\u6240\u5728\u4e86<\/span>\u3002\u4e3b\u8981\u662f\u901a\u8fc7\u6b64\u6ce8\u89e3\uff0c\u80fd\u6240\u6709\u7b26\u5408\u81ea\u52a8\u914d\u7f6e\u6761\u4ef6\u7684bean\u7684\u5b9a\u4e49\u52a0\u8f7d\u5230spring\u5bb9\u5668\u4e2d<\/p>\n<p>@ComponentScan \u8fd9\u4e2a\u719f\u6089spring\u7684\u5f00\u53d1\u8005\u4e5f\u5e94\u8be5\u719f\u6089\uff0c\u4f1a\u626b\u63cf\u5f53\u524d\u5305\u53ca\u5176\u5b50\u5305\u4e0b\u88ab@Component\uff0c@Controller\uff0c@Service\uff0c@Repository\u7b49\u6ce8\u89e3\u6807\u8bb0\u7684\u7c7b\u5e76\u7eb3\u5165\u5230spring\u5bb9\u5668\u4e2d\u8fdb\u884c\u7ba1\u7406\u3002<\/p>\n<\/li>\n<li>@Controller \u548c @RestController&nbsp;\n<p>@RestController \u662fSpring4\u4e4b\u540e\u52a0\u5165\u7684\u6ce8\u89e3\uff0c\u539f\u6765\u5728@Controller\u4e2d\u8fd4\u56dejson\u9700\u8981@ResponseBody\u6765\u914d\u5408\uff0c\u5982\u679c\u76f4\u63a5\u7528@RestController\u66ff\u4ee3@Controller\u5c31\u4e0d\u9700\u8981\u518d\u914d\u7f6e@ResponseBody\uff0c\u9ed8\u8ba4\u8fd4\u56dejson\u683c\u5f0f\u3002\u800c@Controller\u662f\u7528\u6765\u521b\u5efa\u5904\u7406http\u8bf7\u6c42\u7684\u5bf9\u8c61\uff0c\u4e00\u822c\u7ed3\u5408@RequestMapping\u4f7f\u7528\u3002<\/p>\n<\/li>\n<li>@RequestMapping&nbsp;\n<p>\u4e00\u4e2a\u7528\u6765\u5904\u7406\u8bf7\u6c42\u5730\u5740\u6620\u5c04\u7684\u6ce8\u89e3\uff0c\u53ef\u7528\u4e8e\u7c7b\u6216\u65b9\u6cd5\u4e0a\u3002\u7528\u4e8e\u7c7b\u4e0a\uff0c\u8868\u793a\u7c7b\u4e2d\u7684\u6240\u6709\u54cd\u5e94\u8bf7\u6c42\u7684\u65b9\u6cd5\u90fd\u662f\u4ee5\u8be5\u5730\u5740\u4f5c\u4e3a\u7236\u8def\u5f84\u3002<\/p>\n<p>\u5e38\u7528\u5c5e\u6027\uff1avalue\uff0c method\uff0c consumes\uff0cproduces\uff0c params\uff0cheaders\uff1b<br \/>value\uff1a \u6307\u5b9a\u8bf7\u6c42\u7684\u5b9e\u9645\u5730\u5740\uff0c\u6307\u5b9a\u7684\u5730\u5740\u53ef\u4ee5\u662fURI Template \u6a21\u5f0f\uff08\u540e\u9762\u5c06\u4f1a\u8bf4\u660e\uff09\uff1bmethod\uff1a \u6307\u5b9a\u8bf7\u6c42\u7684method\u7c7b\u578b\uff0c GET\u3001POST\u3001PUT\u3001DELETE\u7b49\uff1b<br \/>consumes\uff1a \u6307\u5b9a\u5904\u7406\u8bf7\u6c42\u7684\u63d0\u4ea4\u5185\u5bb9\u7c7b\u578b\uff08Content-Type\uff09\uff0c\u4f8b\u5982application\/json, text\/html;produces: \u6307\u5b9a\u8fd4\u56de\u7684\u5185\u5bb9\u7c7b\u578b\uff0c\u4ec5\u5f53request\u8bf7\u6c42\u5934\u4e2d\u7684(Accept)\u7c7b\u578b\u4e2d\u5305\u542b\u8be5\u6307\u5b9a\u7c7b\u578b\u624d\u8fd4\u56de\uff1b<br \/>params\uff1a \u6307\u5b9arequest\u4e2d<span style=\"color: #ff0000;\">\u5fc5\u987b\u5305\u542b\u67d0\u4e9b\u53c2\u6570\u503c\u662f\uff0c\u624d\u8ba9\u8be5\u65b9\u6cd5\u5904\u7406<\/span>\u3002headers\uff1a \u6307\u5b9arequest\u4e2d<span style=\"color: #ff0000;\">\u5fc5\u987b<\/span>\u5305\u542b\u67d0\u4e9b\u6307\u5b9a\u7684header\u503c\uff0c\u624d\u80fd\u8ba9\u8be5\u65b9\u6cd5\u5904\u7406\u8bf7\u6c42\u3002<\/p>\n<p>\u4e00\u4e2a\u7528\u6765\u5904\u7406\u8bf7\u6c42\u5730\u5740\u6620\u5c04\u7684\u6ce8\u89e3\uff0c\u53ef\u7528\u4e8e\u7c7b\u6216\u65b9\u6cd5\u4e0a\u3002\u7528\u4e8e\u7c7b\u4e0a\uff0c\u8868\u793a\u7c7b\u4e2d\u7684\u6240\u6709\u54cd\u5e94\u8bf7\u6c42\u7684\u65b9\u6cd5\u90fd\u662f\u4ee5\u8be5\u5730\u5740\u4f5c\u4e3a\u7236\u8def\u5f84\u3002<\/p>\n<p>@GetMapping \u7b49\u540c\u4e8e @RequestMapping(method = RequestMethod.GET)<br \/>@PostMapping \u7b49\u540c\u4e8e @RequestMapping(method = RequestMethod.POST)<br \/>@PutMapping \u7b49\u540c\u4e8e @RequestMapping(method = RequestMethod.PUT)<br \/>@DeleteMapping \u7b49\u540c\u4e8e @RequestMapping(method = RequestMethod.DELETE)<br \/>@PatchMapping \u7b49\u540c\u4e8e @RequestMapping(method = RequestMethod.PATCH)&nbsp;<\/p>\n<\/li>\n<li>@RequestBody\u548c@ResponseBody&nbsp;&nbsp;\n<p>@RequestBody\u6ce8\u89e3\u5141\u8bb8request\u7684\u53c2\u6570\u5728reqeust\u4f53\u4e2d\uff0c\u5e38\u5e38\u7ed3\u5408\u524d\u7aefPOST\u8bf7\u6c42\uff0c\u8fdb\u884c\u524d\u540e\u7aef\u4ea4\u4e92\u3002<\/p>\n<p>@ResponseBody\u6ce8\u89e3\u652f\u6301\u5c06\u7684\u53c2\u6570\u5728reqeust\u4f53\u4e2d\uff0c\u901a\u5e38\u8fd4\u56dejson\u683c\u5f0f\u7ed9\u524d\u7aef\u3002<\/p>\n<\/li>\n<li>@PathVariable\u3001@RequestParam&nbsp;\n<p>@PathVariable&nbsp;<br \/>\u5f53\u4f7f\u7528@RequestMapping URI template \u6837\u5f0f\u6620\u5c04\u65f6\uff0c \u5373 someUrl\/{paramId}, \u8fd9\u65f6\u7684paramId\u53ef\u901a\u8fc7 @Pathvariable\u6ce8\u89e3\u7ed1\u5b9a\u5b83\u4f20\u8fc7\u6765\u7684\u503c\u5230\u65b9\u6cd5\u7684\u53c2\u6570\u4e0a\u3002<br \/><img decoding=\"async\" src=\"http:\/\/cdn.langmanezhuang.com\/blog\/20221126\/156-543925-20210224212050365-1180446291.png\" alt=\"\" loading=\"lazy\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/li>\n<li>@Component\u3001@Service\u3001@Repository&nbsp;\n<p>@Component \u6700\u666e\u901a\u7684\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u88ab\u6ce8\u5165\u5230spring\u5bb9\u5668\u8fdb\u884c\u7ba1\u7406<br \/>@Repository \u4f5c\u7528\u4e8e\u6301\u4e45\u5c42<br \/>@Service \u4f5c\u7528\u4e8e\u4e1a\u52a1\u903b\u8f91\u5c42<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>1.Springboot\u4ecb\u7ecd &nbsp;Spring Boot\u662f\u7531Pivotal\u56e2\u961f\u63d0\u4f9b\u7684\u5168\u65b0\u6846\u67b6\uff0c\u5176\u8bbe\u8ba1\u76ee &hellip; <a href=\"http:\/\/blog.langmanezhuang.com\/index.php\/2021\/02\/23\/springboot%e5%9f%ba%e7%a1%80%e6%a0%b8%e5%bf%83\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">Springboot\u57fa\u7840\u6838\u5fc3<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/posts\/883"}],"collection":[{"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/comments?post=883"}],"version-history":[{"count":0,"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/posts\/883\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/media?parent=883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/categories?post=883"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.langmanezhuang.com\/index.php\/wp-json\/wp\/v2\/tags?post=883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}