{"id":1262,"date":"2019-08-27T16:50:06","date_gmt":"2019-08-27T08:50:06","guid":{"rendered":"https:\/\/199604.com\/?p=1262"},"modified":"2019-08-27T16:50:06","modified_gmt":"2019-08-27T08:50:06","slug":"%e8%ae%b0%e5%bf%86%e7%b3%bb%e5%88%97-spring-boot-%e6%b3%a8%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/199604.com\/1262","title":{"rendered":"\u8bb0\u5fc6\u7cfb\u5217-Spring Boot \u6ce8\u89e3"},"content":{"rendered":"<p>\u88f8\u8f9e\u9700\u8981\u52c7\u6c14~\u88f8\u8f9e\u4e00\u65f6\u723d,\u4e00\u76f4\u88f8\u8f9e\u4e00\u76f4\u723d?<\/p>\n<p>\u5df2\u7ecf\u5feb\u4e24\u4e2a\u6708\u6ca1\u6709\u627e\u5230\u5de5\u4f5c\u4e86\u5f00\u5fc3\u5427~<\/p>\n<hr \/>\n<h2>1.\u6ce8\u89e3(annotations)\u5217\u8868<\/h2>\n<p>@SpringBootApplication\uff1a\u5305\u542b\u4e86@ComponentScan\u3001@Configuration\u548c@EnableAutoConfiguration\u6ce8\u89e3\u3002\u5176\u4e2d@ComponentScan\u8ba9spring Boot\u626b\u63cf\u5230Configuration\u7c7b\u5e76\u628a\u5b83\u52a0\u5165\u5230\u7a0b\u5e8f\u4e0a\u4e0b\u6587\u3002<\/p>\n<p>@Configuration \u7b49\u540c\u4e8espring\u7684XML\u914d\u7f6e\u6587\u4ef6\uff1b\u4f7f\u7528Java\u4ee3\u7801\u53ef\u4ee5\u68c0\u67e5\u7c7b\u578b\u5b89\u5168\u3002<\/p>\n<p>@EnableAutoConfiguration \u81ea\u52a8\u914d\u7f6e<\/p>\n<p>@ComponentScan \u7ec4\u4ef6\u626b\u63cf\uff0c\u53ef\u81ea\u52a8\u53d1\u73b0\u548c\u88c5\u914d\u4e00\u4e9bBean\u3002<\/p>\n<p>@Component\u53ef\u914d\u5408CommandLineRunner\u4f7f\u7528\uff0c\u5728\u7a0b\u5e8f\u542f\u52a8\u540e\u6267\u884c\u4e00\u4e9b\u57fa\u7840\u4efb\u52a1\u3002<\/p>\n<p>@RestController\u6ce8\u89e3\u662f@Controller\u548c@ResponseBody\u7684\u5408\u96c6,\u8868\u793a\u8fd9\u662f\u4e2a\u63a7\u5236\u5668bean,\u5e76\u4e14\u662f\u5c06\u51fd\u6570\u7684\u8fd4\u56de\u503c\u76f4 \u63a5\u586b\u5165HTTP\u54cd\u5e94\u4f53\u4e2d,\u662fREST\u98ce\u683c\u7684\u63a7\u5236\u5668\u3002<\/p>\n<p>@Autowired\u81ea\u52a8\u5bfc\u5165\u3002<\/p>\n<p>@PathVariable\u83b7\u53d6\u53c2\u6570\u3002<\/p>\n<p>@JsonBackReference\u89e3\u51b3\u5d4c\u5957\u5916\u94fe\u95ee\u9898\u3002<\/p>\n<p>@RepositoryRestResourcepublic\u914d\u5408spring-boot-starter-data-rest\u4f7f\u7528\u3002<\/p>\n<h2>2.\u6ce8\u89e3(annotations)\u8be6\u89e3<\/h2>\n<p>@SpringBootApplication\uff1a\u7533\u660e\u8ba9spring boot\u81ea\u52a8\u7ed9\u7a0b\u5e8f\u8fdb\u884c\u5fc5\u8981\u7684\u914d\u7f6e\uff0c\u8fd9\u4e2a\u914d\u7f6e\u7b49\u540c\u4e8e\uff1a@Configuration \uff0c@EnableAutoConfiguration \u548c @ComponentScan \u4e09\u4e2a\u914d\u7f6e\u3002<\/p>\n<pre class=\"lang:default decode:true\">package com.example.myproject; \r\nimport org.springframework.boot.SpringApplication; \r\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\r\n\r\n@SpringBootApplication \/\/ same as @Configuration @EnableAutoConfiguration @ComponentScan \r\npublic class Application { \r\n\tpublic static void main(String[] args) { \r\n\t\tSpringApplication.run(Application.class, args); \r\n\t} \r\n}<\/pre>\n<p>@ResponseBody\uff1a\u8868\u793a\u8be5\u65b9\u6cd5\u7684\u8fd4\u56de\u7ed3\u679c\u76f4\u63a5\u5199\u5165HTTP response body\u4e2d\uff0c\u4e00\u822c\u5728\u5f02\u6b65\u83b7\u53d6\u6570\u636e\u65f6\u4f7f\u7528\uff0c\u7528\u4e8e\u6784\u5efaRESTful\u7684api\u3002\u5728\u4f7f\u7528@RequestMapping\u540e\uff0c\u8fd4\u56de\u503c\u901a\u5e38\u89e3\u6790\u4e3a\u8df3\u8f6c\u8def\u5f84\uff0c\u52a0\u4e0a@responsebody\u540e\u8fd4\u56de\u7ed3\u679c\u4e0d\u4f1a\u88ab\u89e3\u6790\u4e3a\u8df3\u8f6c\u8def\u5f84\uff0c\u800c\u662f\u76f4\u63a5\u5199\u5165HTTP response body\u4e2d\u3002\u6bd4\u5982\u5f02\u6b65\u83b7\u53d6json\u6570\u636e\uff0c\u52a0\u4e0a@responsebody\u540e\uff0c\u4f1a\u76f4\u63a5\u8fd4\u56dejson\u6570\u636e\u3002\u8be5\u6ce8\u89e3\u4e00\u822c\u4f1a\u914d\u5408@RequestMapping\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n<pre class=\"lang:default decode:true\">@RequestMapping(\u201c\/test\u201d) \r\n@ResponseBody \r\npublic String test(){ \r\n\treturn \u201dok\u201d; \r\n}<\/pre>\n<p>@Controller\uff1a\u7528\u4e8e\u5b9a\u4e49\u63a7\u5236\u5668\u7c7b\uff0c\u5728spring \u9879\u76ee\u4e2d\u7531\u63a7\u5236\u5668\u8d1f\u8d23\u5c06\u7528\u6237\u53d1\u6765\u7684URL\u8bf7\u6c42\u8f6c\u53d1\u5230\u5bf9\u5e94\u7684\u670d\u52a1\u63a5\u53e3\uff08service\u5c42\uff09\uff0c\u4e00\u822c\u8fd9\u4e2a\u6ce8\u89e3\u5728\u7c7b\u4e2d\uff0c\u901a\u5e38\u65b9\u6cd5\u9700\u8981\u914d\u5408\u6ce8\u89e3@RequestMapping\u3002<\/p>\n<pre class=\"lang:default decode:true \">@Controller \r\n@RequestMapping(\u201c\/demoInfo\u201d) \r\npublicclass DemoController { \r\n\t@Autowired \r\n\tprivate DemoInfoService demoInfoService;\r\n\r\n\t@RequestMapping(\"\/hello\")\r\n\tpublic String hello(Map&lt;String,Object&gt; map){\r\n\t   System.out.println(\"DemoController.hello()\");\r\n\t   map.put(\"hello\",\"from TemplateController.helloHtml\");\r\n\t   \/\/\u4f1a\u4f7f\u7528hello.html\u6216\u8005hello.ftl\u6a21\u677f\u8fdb\u884c\u6e32\u67d3\u663e\u793a.\r\n\t   return \"hello\";\r\n\t}\r\n}<\/pre>\n<p>@RestController\uff1a\u7528\u4e8e\u6807\u6ce8\u63a7\u5236\u5c42\u7ec4\u4ef6(\u5982struts\u4e2d\u7684action)\uff0c@ResponseBody\u548c@Controller\u7684\u5408\u96c6<\/p>\n<pre class=\"lang:default decode:true \">package com.glj.demo.web;\r\nimport org.springframework.web.bind.annotation.RequestMapping; \r\nimport org.springframework.web.bind.annotation.RestController;\r\n\r\n@RestController \r\n@RequestMapping(\u201c\/demoInfo2\u201d) \r\npublicclass DemoController2 {\r\n\r\n\t@RequestMapping(\"\/test\")\r\n\tpublic String test(){\r\n\t   return \"ok\";\r\n\t}\r\n}<\/pre>\n<p>@RequestMapping\uff1a\u63d0\u4f9b\u8def\u7531\u4fe1\u606f\uff0c\u8d1f\u8d23URL\u5230Controller\u4e2d\u7684\u5177\u4f53\u51fd\u6570\u7684\u6620\u5c04\u3002<\/p>\n<p>@EnableAutoConfiguration\uff1aSpring Boot\u81ea\u52a8\u914d\u7f6e\uff08auto-configuration\uff09\uff1a\u5c1d\u8bd5\u6839\u636e\u4f60\u6dfb\u52a0\u7684jar\u4f9d\u8d56\u81ea\u52a8\u914d\u7f6e\u4f60\u7684Spring\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684classpath\u4e0b\u5b58\u5728HSQLDB\uff0c\u5e76\u4e14\u4f60\u6ca1\u6709\u624b\u52a8\u914d\u7f6e\u4efb\u4f55\u6570\u636e\u5e93\u8fde\u63a5beans\uff0c\u90a3\u4e48\u6211\u4eec\u5c06\u81ea\u52a8\u914d\u7f6e\u4e00\u4e2a\u5185\u5b58\u578b\uff08in-memory\uff09\u6570\u636e\u5e93\u201d\u3002\u4f60\u53ef\u4ee5\u5c06@EnableAutoConfiguration\u6216\u8005@SpringBootApplication\u6ce8\u89e3\u6dfb\u52a0\u5230\u4e00\u4e2a@Configuration\u7c7b\u4e0a\u6765\u9009\u62e9\u81ea\u52a8\u914d\u7f6e\u3002\u5982\u679c\u53d1\u73b0\u5e94\u7528\u4e86\u4f60\u4e0d\u60f3\u8981\u7684\u7279\u5b9a\u81ea\u52a8\u914d\u7f6e\u7c7b\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528@EnableAutoConfiguration\u6ce8\u89e3\u7684\u6392\u9664\u5c5e\u6027\u6765\u7981\u7528\u5b83\u4eec\u3002<\/p>\n<p>@ComponentScan\uff1a\u8868\u793a\u5c06\u8be5\u7c7b\u81ea\u52a8\u53d1\u73b0\u626b\u63cf\u7ec4\u4ef6\u3002\u4e2a\u4eba\u7406\u89e3\u76f8\u5f53\u4e8e\uff0c\u5982\u679c\u626b\u63cf\u5230\u6709@Component\u3001@Controller\u3001@Service\u7b49\u8fd9\u4e9b\u6ce8\u89e3\u7684\u7c7b\uff0c\u5e76\u6ce8\u518c\u4e3aBean\uff0c\u53ef\u4ee5\u81ea\u52a8\u6536\u96c6\u6240\u6709\u7684Spring\u7ec4\u4ef6\uff0c\u5305\u62ec@Configuration\u7c7b\u3002\u6211\u4eec\u7ecf\u5e38\u4f7f\u7528@ComponentScan\u6ce8\u89e3\u641c\u7d22beans\uff0c\u5e76\u7ed3\u5408@Autowired\u6ce8\u89e3\u5bfc\u5165\u3002\u53ef\u4ee5\u81ea\u52a8\u6536\u96c6\u6240\u6709\u7684Spring\u7ec4\u4ef6\uff0c\u5305\u62ec@Configuration\u7c7b\u3002\u6211\u4eec\u7ecf\u5e38\u4f7f\u7528@ComponentScan\u6ce8\u89e3\u641c\u7d22beans\uff0c\u5e76\u7ed3\u5408@Autowired\u6ce8\u89e3\u5bfc\u5165\u3002\u5982\u679c\u6ca1\u6709\u914d\u7f6e\u7684\u8bdd\uff0cSpring Boot\u4f1a\u626b\u63cf\u542f\u52a8\u7c7b\u6240\u5728\u5305\u4e0b\u4ee5\u53ca\u5b50\u5305\u4e0b\u7684\u4f7f\u7528\u4e86@Service,@Repository\u7b49\u6ce8\u89e3\u7684\u7c7b\u3002<\/p>\n<p>@Configuration\uff1a\u76f8\u5f53\u4e8e\u4f20\u7edf\u7684xml\u914d\u7f6e\u6587\u4ef6\uff0c\u5982\u679c\u6709\u4e9b\u7b2c\u4e09\u65b9\u5e93\u9700\u8981\u7528\u5230xml\u6587\u4ef6\uff0c\u5efa\u8bae\u4ecd\u7136\u901a\u8fc7@Configuration\u7c7b\u4f5c\u4e3a\u9879\u76ee\u7684\u914d\u7f6e\u4e3b\u7c7b\u2014\u2014\u53ef\u4ee5\u4f7f\u7528@ImportResource\u6ce8\u89e3\u52a0\u8f7dxml\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<p>@Import\uff1a\u7528\u6765\u5bfc\u5165\u5176\u4ed6\u914d\u7f6e\u7c7b<\/p>\n<p>@ImportResource\uff1a\u7528\u6765\u52a0\u8f7dxml\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<p>@Autowired\uff1a\u81ea\u52a8\u5bfc\u5165\u4f9d\u8d56\u7684bean<\/p>\n<p>@Service\uff1a\u4e00\u822c\u7528\u4e8e\u4fee\u9970service\u5c42\u7684\u7ec4\u4ef6<\/p>\n<p>@Repository\uff1a\u4f7f\u7528@Repository\u6ce8\u89e3\u53ef\u4ee5\u786e\u4fddDAO\u6216\u8005repositories\u63d0\u4f9b\u5f02\u5e38\u8f6c\u8bd1\uff0c\u8fd9\u4e2a\u6ce8\u89e3\u4fee\u9970\u7684DAO\u6216\u8005repositories\u7c7b\u4f1a\u88abComponetScan\u53d1\u73b0\u5e76\u914d\u7f6e\uff0c\u540c\u65f6\u4e5f\u4e0d\u9700\u8981\u4e3a\u5b83\u4eec\u63d0\u4f9bXML\u914d\u7f6e\u9879\u3002<\/p>\n<p>@Bean\uff1a\u7528@Bean\u6807\u6ce8\u65b9\u6cd5\u7b49\u4ef7\u4e8eXML\u4e2d\u914d\u7f6e\u7684bean\u3002<\/p>\n<p>@Value\uff1a\u6ce8\u5165Spring boot application.properties\u914d\u7f6e\u7684\u5c5e\u6027\u7684\u503c\u3002<\/p>\n<pre class=\"lang:default decode:true\">@Value(value = \"${user.userName}\")\r\n\/\/@Value(\"${user.userName}\")\r\nprivate String userName;\r\n\u53ef\u4ee5\u770b\u5f97value\u6ce8\u89e3\u7684\u4f7f\u7528\u65b9\u5f0f\uff1a@Value(value = \u201c${user.userName}\u201d)\uff0c\r\n\u8fd9\u4e2a\u65f6\u5019\u5c31\u4f1a\u628a\u914d\u7f6e\u6587\u4ef6\u91cc\u7684user.userName\u7684\u503c\u8d4b\u503c\u7ed9\u53d8\u91cfuserName<\/pre>\n<p>@Inject\uff1a\u7b49\u4ef7\u4e8e\u9ed8\u8ba4\u7684@Autowired\uff0c\u53ea\u662f\u6ca1\u6709required\u5c5e\u6027\uff1b<\/p>\n<p>@Component\uff1a\u6cdb\u6307\u7ec4\u4ef6\uff0c\u5f53\u7ec4\u4ef6\u4e0d\u597d\u5f52\u7c7b\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u6ce8\u89e3\u8fdb\u884c\u6807\u6ce8\u3002<\/p>\n<p>@Bean:\u76f8\u5f53\u4e8eXML\u4e2d\u7684,\u653e\u5728\u65b9\u6cd5\u7684\u4e0a\u9762\uff0c\u800c\u4e0d\u662f\u7c7b\uff0c\u610f\u601d\u662f\u4ea7\u751f\u4e00\u4e2abean,\u5e76\u4ea4\u7ed9spring\u7ba1\u7406\u3002<\/p>\n<p>@Resource(name=\u201dname\u201d,type=\u201dtype\u201d)\uff1a\u6ca1\u6709\u62ec\u53f7\u5185\u5185\u5bb9\u7684\u8bdd\uff0c\u9ed8\u8ba4byName\u3002\u4e0e@Autowired\u5e72\u7c7b\u4f3c\u7684\u4e8b\u3002<\/p>\n<p>@AutoWired\uff1a\u81ea\u52a8\u5bfc\u5165\u4f9d\u8d56\u7684bean\u3002byType\u65b9\u5f0f\u3002\u628a\u914d\u7f6e\u597d\u7684Bean\u62ff\u6765\u7528\uff0c\u5b8c\u6210\u5c5e\u6027\u3001\u65b9\u6cd5\u7684\u7ec4\u88c5\uff0c\u5b83\u53ef\u4ee5\u5bf9\u7c7b\u6210\u5458\u53d8\u91cf\u3001\u65b9\u6cd5\u53ca\u6784\u9020\u51fd\u6570\u8fdb\u884c\u6807\u6ce8\uff0c\u5b8c\u6210\u81ea\u52a8\u88c5\u914d\u7684\u5de5\u4f5c\u3002\u5f53\u52a0\u4e0a\uff08required=false\uff09\u65f6\uff0c\u5c31\u7b97\u627e\u4e0d\u5230bean\u4e5f\u4e0d\u62a5\u9519\u3002<\/p>\n<p>@Qualifier\uff1a\u5f53\u6709\u591a\u4e2a\u540c\u4e00\u7c7b\u578b\u7684Bean\u65f6\uff0c\u53ef\u4ee5\u7528@Qualifier(\u201cname\u201d)\u6765\u6307\u5b9a\u3002\u4e0e@Autowired\u914d\u5408\u4f7f\u7528\u3002@Qualifier\u9650\u5b9a\u63cf\u8ff0\u7b26\u9664\u4e86\u80fd\u6839\u636e\u540d\u5b57\u8fdb\u884c\u6ce8\u5165\uff0c\u4f46\u80fd\u8fdb\u884c\u66f4\u7ec6\u7c92\u5ea6\u7684\u63a7\u5236\u5982\u4f55\u9009\u62e9\u5019\u9009\u8005\uff0c\u5177\u4f53\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true\">@Autowired \r\n@Qualifier(value = \u201cdemoInfoService\u201d) \r\nprivate DemoInfoService demoInfoService;<\/pre>\n<h2>3.JPA\u6ce8\u89e3<\/h2>\n<p>@Entity\uff1a@Table(name=\u201d\u201c)\uff1a\u8868\u660e\u8fd9\u662f\u4e00\u4e2a\u5b9e\u4f53\u7c7b\u3002\u4e00\u822c\u7528\u4e8ejpa\u8fd9\u4e24\u4e2a\u6ce8\u89e3\u4e00\u822c\u4e00\u5757\u4f7f\u7528\uff0c\u4f46\u662f\u5982\u679c\u8868\u540d\u548c\u5b9e\u4f53\u7c7b\u540d\u76f8\u540c\u7684\u8bdd\uff0c@Table\u53ef\u4ee5\u7701\u7565<\/p>\n<p>@MappedSuperClass:\u7528\u5728\u786e\u5b9a\u662f\u7236\u7c7b\u7684entity\u4e0a\u3002\u7236\u7c7b\u7684\u5c5e\u6027\u5b50\u7c7b\u53ef\u4ee5\u7ee7\u627f\u3002<\/p>\n<p>@NoRepositoryBean:\u4e00\u822c\u7528\u4f5c\u7236\u7c7b\u7684repository\uff0c\u6709\u8fd9\u4e2a\u6ce8\u89e3\uff0cspring\u4e0d\u4f1a\u53bb\u5b9e\u4f8b\u5316\u8be5repository\u3002<\/p>\n<p>@Column\uff1a\u5982\u679c\u5b57\u6bb5\u540d\u4e0e\u5217\u540d\u76f8\u540c\uff0c\u5219\u53ef\u4ee5\u7701\u7565\u3002<\/p>\n<p>@Id\uff1a\u8868\u793a\u8be5\u5c5e\u6027\u4e3a\u4e3b\u952e\u3002<\/p>\n<p>@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = \u201crepair_seq\u201d)\uff1a\u8868\u793a\u4e3b\u952e\u751f\u6210\u7b56\u7565\u662fsequence\uff08\u53ef\u4ee5\u4e3aAuto\u3001IDENTITY\u3001native\u7b49\uff0cAuto\u8868\u793a\u53ef\u5728\u591a\u4e2a\u6570\u636e\u5e93\u95f4\u5207\u6362\uff09\uff0c\u6307\u5b9asequence\u7684\u540d\u5b57\u662frepair_seq\u3002<\/p>\n<p>@SequenceGeneretor(name = \u201crepair_seq\u201d, sequenceName = \u201cseq_repair\u201d, allocationSize = 1)\uff1aname\u4e3asequence\u7684\u540d\u79f0\uff0c\u4ee5\u4fbf\u4f7f\u7528\uff0csequenceName\u4e3a\u6570\u636e\u5e93\u7684sequence\u540d\u79f0\uff0c\u4e24\u4e2a\u540d\u79f0\u53ef\u4ee5\u4e00\u81f4\u3002<\/p>\n<p>@Transient\uff1a\u8868\u793a\u8be5\u5c5e\u6027\u5e76\u975e\u4e00\u4e2a\u5230\u6570\u636e\u5e93\u8868\u7684\u5b57\u6bb5\u7684\u6620\u5c04,ORM\u6846\u67b6\u5c06\u5ffd\u7565\u8be5\u5c5e\u6027\u3002\u5982\u679c\u4e00\u4e2a\u5c5e\u6027\u5e76\u975e\u6570\u636e\u5e93\u8868\u7684\u5b57\u6bb5\u6620\u5c04,\u5c31\u52a1\u5fc5\u5c06\u5176\u6807\u793a\u4e3a@Transient,\u5426\u5219,ORM\u6846\u67b6\u9ed8\u8ba4\u5176\u6ce8\u89e3\u4e3a@Basic\u3002@Basic(fetch=FetchType.LAZY)\uff1a\u6807\u8bb0\u53ef\u4ee5\u6307\u5b9a\u5b9e\u4f53\u5c5e\u6027\u7684\u52a0\u8f7d\u65b9\u5f0f<\/p>\n<p>@JsonIgnore\uff1a\u4f5c\u7528\u662fjson\u5e8f\u5217\u5316\u65f6\u5c06Java\u00a0bean\u4e2d\u7684\u4e00\u4e9b\u5c5e\u6027\u5ffd\u7565\u6389,\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u90fd\u53d7\u5f71\u54cd\u3002<\/p>\n<p>@JoinColumn\uff08name=\u201dloginId\u201d\uff09:\u4e00\u5bf9\u4e00\uff1a\u672c\u8868\u4e2d\u6307\u5411\u53e6\u4e00\u4e2a\u8868\u7684\u5916\u952e\u3002\u4e00\u5bf9\u591a\uff1a\u53e6\u4e00\u4e2a\u8868\u6307\u5411\u672c\u8868\u7684\u5916\u952e\u3002<\/p>\n<p>@OneToOne\u3001@OneToMany\u3001@ManyToOne\uff1a\u5bf9\u5e94hibernate\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u4e00\u5bf9\u4e00\uff0c\u4e00\u5bf9\u591a\uff0c\u591a\u5bf9\u4e00\u3002<\/p>\n<h2>4.spring mvc\u76f8\u5173\u6ce8\u89e3<\/h2>\n<p>@RequestMapping\uff1a@RequestMapping(\u201c\/path\u201d)\u8868\u793a\u8be5\u63a7\u5236\u5668\u5904\u7406\u6240\u6709\u201c\/path\u201d\u7684URL\u8bf7\u6c42\u3002RequestMapping\u662f\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<br \/>\n\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>\u8be5\u6ce8\u89e3\u6709\u516d\u4e2a\u5c5e\u6027\uff1a<br \/>\nparams:\u6307\u5b9arequest\u4e2d\u5fc5\u987b\u5305\u542b\u67d0\u4e9b\u53c2\u6570\u503c\u662f\uff0c\u624d\u8ba9\u8be5\u65b9\u6cd5\u5904\u7406\u3002<br \/>\nheaders:\u6307\u5b9arequest\u4e2d\u5fc5\u987b\u5305\u542b\u67d0\u4e9b\u6307\u5b9a\u7684header\u503c\uff0c\u624d\u80fd\u8ba9\u8be5\u65b9\u6cd5\u5904\u7406\u8bf7\u6c42\u3002<br \/>\nvalue:\u6307\u5b9a\u8bf7\u6c42\u7684\u5b9e\u9645\u5730\u5740\uff0c\u6307\u5b9a\u7684\u5730\u5740\u53ef\u4ee5\u662fURI Template \u6a21\u5f0f<br \/>\nmethod:\u6307\u5b9a\u8bf7\u6c42\u7684method\u7c7b\u578b\uff0c GET\u3001POST\u3001PUT\u3001DELETE\u7b49<br \/>\nconsumes:\u6307\u5b9a\u5904\u7406\u8bf7\u6c42\u7684\u63d0\u4ea4\u5185\u5bb9\u7c7b\u578b\uff08Content-Type\uff09\uff0c\u5982application\/json,text\/html;<br \/>\nproduces:\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\u3002<\/p>\n<p>@RequestParam\uff1a\u7528\u5728\u65b9\u6cd5\u7684\u53c2\u6570\u524d\u9762<\/p>\n<p>url\u8bf7\u6c42\uff1ahttp:\/\/localhost:8080\/findUser?username=glj<\/p>\n<pre class=\"lang:default decode:true \">@RequestMapping(value=\"\/findUser\", method = RequestMethod.GET)\r\n    public String findUser(@RequestParam(value=\"username\",defaultValue=\"glj\") String userName){\r\n        System.out.println(\"username: \" + userName);\r\n        return \"hello\";\r\n    }<\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u5c06\u8bf7\u6c42\u4e2d\u7684username\u53c2\u6570\u7684\u503c\u8d4b\u7ed9userName\u53d8\u91cf\u3002\u7b49\u4ef7\u4e8e\uff1a<\/p>\n<pre class=\"lang:default decode:true \">@RequestMapping(value=\"\/findUser\", method = RequestMethod.GET)\r\n    public String findUser(HttpServletRequest request){\r\n\t\tString username = request.getParameter(\"username\");\r\n        System.out.println(\"username: \" + username);\r\n        return \"hello\";\r\n    }<\/pre>\n<p>@PathVariable:\u8def\u5f84\u53d8\u91cf<\/p>\n<pre class=\"lang:default decode:true \">@RequestMapping(\"\/user\/get\/mac\/{macAddress}\") \r\npublic String getByMacAddress(@PathVariable String macAddress){ \r\n\/\/do something; \r\n}<\/pre>\n<p>https:\/\/199604.com\/1254 &#8211;&gt;Spring MVC\u5e38\u7528\u6ce8\u89e3<\/p>\n<h2>5.\u5168\u5c40\u5f02\u5e38\u5904\u7406<\/h2>\n<p>@ControllerAdvice\uff1a\u5305\u542b@Component\u3002\u53ef\u4ee5\u88ab\u626b\u63cf\u5230\u3002\u7edf\u4e00\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<p>@ExceptionHandler\uff08Exception.class\uff09\uff1a\u7528\u5728\u65b9\u6cd5\u4e0a\u9762\u8868\u793a\u9047\u5230\u8fd9\u4e2a\u5f02\u5e38\u5c31\u6267\u884c\u4ee5\u4e0b\u65b9\u6cd5\u3002<\/p>\n<hr \/>\n<p>\u8f6c\u8f7d\uff1ahttps:\/\/www.cnblogs.com\/tanwei81\/p\/6814022.html \u4f5c\u8005\uff1a\u00a0<a href=\"https:\/\/home.cnblogs.com\/u\/tanwei81\/\">tanwei81\u00a0<\/a><\/p>\n<hr \/>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/qn.199604.com\/wp-content\/uploads\/2017\/07\/zhifubao.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large\" data-original=\"http:\/\/qn.199604.com\/wp-content\/uploads\/2017\/07\/zhifubao.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" width=\"318\" height=\"406\" \/><\/div><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/qn.199604.com\/wp-content\/uploads\/2017\/07\/weixin.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large\" data-original=\"http:\/\/qn.199604.com\/wp-content\/uploads\/2017\/07\/weixin.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" width=\"268\" height=\"291\" \/><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u88f8\u8f9e\u9700\u8981\u52c7\u6c14~\u88f8\u8f9e\u4e00\u65f6\u723d,\u4e00\u76f4\u88f8\u8f9e\u4e00\u76f4\u723d? \u5df2\u7ecf\u5feb\u4e24\u4e2a\u6708\u6ca1\u6709\u627e\u5230\u5de5\u4f5c\u4e86\u5f00\u5fc3\u5427~ 1.\u6ce8\u89e3(annotation [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[216],"class_list":["post-1262","post","type-post","status-publish","format-standard","hentry","category-2","tag-spring-boot"],"_links":{"self":[{"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/posts\/1262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/comments?post=1262"}],"version-history":[{"count":1,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/posts\/1262\/revisions"}],"predecessor-version":[{"id":1263,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/posts\/1262\/revisions\/1263"}],"wp:attachment":[{"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/media?parent=1262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/categories?post=1262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/tags?post=1262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}