{"id":3578,"date":"2018-12-17T17:19:46","date_gmt":"2018-12-17T09:19:46","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3578"},"modified":"2018-12-17T17:19:46","modified_gmt":"2018-12-17T09:19:46","slug":"%e8%af%a6%e8%a7%a3-requestmapping-%e4%b8%ad%e7%9a%84-consumes-%e5%92%8c-produces","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3578","title":{"rendered":"\u8be6\u89e3 @RequestMapping \u4e2d\u7684 consumes \u548c produces"},"content":{"rendered":"<p>\n\t@RequestMapping \u6ce8\u89e3\u975e\u5e38\u7684\u5f3a\u5927\uff0cSpring MVC \u548c SpringBoot \u4e2d\u90fd\u4f1a\u7528\u5230\u8fd9\u4e2a\u6ce8\u89e3\u3002\u8981\u5b66\u4f1a @RequestMapping \u7684\u7528\u6cd5\uff0c\u5c31\u9700\u8981\u4ece\u5b83\u7684\u539f\u7406\u548c\u5b9e\u73b0\u673a\u5236\u8bf4\u8d77\u3002\u672c\u6587\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u6252\u4e00\u6252 @RequestMapping \u7684\u795e\u79d8\u9762\u7eb1\u3002\n<\/p>\n<p>\n\t@RequestMapping \u662f\u7528\u6765\u6620\u5c04\u8bf7\u6c42\uff0c\u53ef\u4ee5\u914d\u7f6e\u5728\u7c7b\u4e0a\uff0c\u4e5f\u53ef\u4ee5\u914d\u7f6e\u5728\u65b9\u6cd5\u4e0a\u3002\u8bf4\u5230\u6620\u5c04\u8bf7\u6c42\uff0c\u6211\u4eec\u5c31\u5e94\u8be5\u60f3\u5230 Servlet \u5728 web.xml \u4e2d\u7684\u914d\u7f6e\u3002\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;servlet&gt;\r\n    &lt;servlet-name&gt;xttblog&lt;\/servlet-name&gt;\r\n    &lt;servlet-class&gt;com.xttblog.XttblogController&lt;\/servlet-class&gt;\r\n&lt;\/servlet&gt;\r\n&lt;servlet-mapping&gt;\r\n    &lt;servlet-name&gt;xttblog&lt;\/servlet-name&gt;\r\n    &lt;url-pattern&gt;\/xttblog&lt;\/url-pattern&gt;\r\n&lt;\/servlet-mapping&gt;<\/pre>\n<p>\n\t@RequestMapping \u6ce8\u89e3\u7684\u4f5c\u7528\u5c31\u548c\u4e0a\u9762\u7684 web.xml \u4e2d\u914d\u7f6e Servlet \u6620\u5c04\u7684\u4f5c\u7528\u7c7b\u4f3c\u3002\n<\/p>\n<p>\n\t\u4e86\u89e3\u4e86 @RequestMapping \u7684\u4f5c\u7528\uff0c\u6211\u4eec\u518d\u6765\u770b\u770b @RequestMapping \u6ce8\u89e3\u7684\u6e90\u7801\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\n@Target({ElementType.METHOD, ElementType.TYPE})\r\n@Retention(RetentionPolicy.RUNTIME)\r\n@Documented\r\n@Mapping\r\npublic @interface RequestMapping {\r\n    String name() default &quot;&quot;;\r\n    String[] value() default {};\r\n    String[] path() default {};\r\n    RequestMethod[] method() default {};\r\n    String[] params() default {};\r\n    String[] headers() default {};\r\n    String[] consumes() default {};\r\n    String[] produces() default {};\r\n}<\/pre>\n<p>\n\t\u5bf9\u4e0a\u9762\u7684\u6ce8\u89e3\uff0c\u7b80\u5355\u89e3\u91ca\u4e00\u4e0b\u3002\n<\/p>\n<p>\n\t\u5728 @Target \u4e2d\u6709\u4e24\u4e2a\u5c5e\u6027\uff0c\u5206\u522b\u4e3a ElementType.METHOD \u548c ElementType.TYPE\u3002\u8fd9\u8868\u660e\u4e86 @RequestMapping \u6ce8\u89e3\u53ef\u4ee5\u4f5c\u7528\u5728\u7c7b\u548c\u65b9\u6cd5\u4e0a\u3002\n<\/p>\n<p>\n\t@RequestMapping \u6ce8\u89e3\u4e2d\u7684\u5c5e\u6027\u9664\u4e86 name \u8fd4\u56de\u7684\u5b57\u7b26\u4e32\uff0c\u5176\u5b83\u7684\u65b9\u6cd5\u5747\u8fd4\u56de\u6570\u7ec4\uff0c\u4e5f\u5c31\u662f\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u5c5e\u6027\u503c\u3002\n<\/p>\n<p>\n\t\u5c06 @RequestMapping \u6ce8\u89e3\u5728\u65b9\u6cd5\u4e0a\uff0c\u800c Controller \u4e0a\u672a\u6dfb\u52a0 @RequestMapping \u6ce8\u89e3\u65f6\uff0c\u8fd9\u65f6\u7684\u8bf7\u6c42 URL \u662f\u76f8\u5bf9\u4e8e Web \u6839\u76ee\u5f55 \/\u3002\n<\/p>\n<p>\n\t@RequestMapping \u4e2d\u7684 method \u4e3b\u8981\u7528\u6765\u5b9a\u4e49\u63a5\u6536\u6d4f\u89c8\u5668\u53d1\u6765\u7684\u4f55\u79cd\u8bf7\u6c42\u3002\u4f7f\u7528\u679a\u4e3e\u7c7b org.springframework.web.bind.annotation.RequestMethod \u6765\u5b9a\u4e49\u6d4f\u89c8\u5668\u8bf7\u6c42\u7684\u65b9\u5f0f\u3002\u6211\u4eec\u5e38\u7528\u7684\u6709 4 \u79cd\u65b9\u5f0f\uff0c\u5206\u522b\u662f\uff1aGET\uff08\u67e5\uff09\u3001POST\uff08\u589e\uff09\u3001PUT\uff08\u6539\uff09\u3001DELETE\uff08\u5220\uff09\u3002\n<\/p>\n<p>\n\tmethod \u8fd4\u56de\u7684\u4e5f\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u56e0\u6b64\u4e5f\u53ef\u4ee5\u6307\u5b9a\u591a\u4e2a\u3002\u5982\u4e0b\u6240\u793a\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\n@Controller\r\n@RequestMapping(path = &quot;\/xttblog&quot;)\r\npublic class XttblogController {\r\n    \/\/ \u8be5\u65b9\u6cd5\u5c06\u540c\u65f6\u63a5\u6536\u901a\u8fc7GET\u548cPOST\u65b9\u5f0f\u53d1\u6765\u7684\u8bf7\u6c42\r\n    @RequestMapping(path = &quot;\/login&quot;, method={RequestMethod.POST,RequestMethod.GET})\r\n    public String login() {\r\n        return &quot;success&quot;;\r\n    }\r\n}<\/pre>\n<p>\n\t@RequestMapping \u7684 params \u5c5e\u6027\uff0c\u8be5\u5c5e\u6027\u8868\u793a\u8bf7\u6c42\u53c2\u6570\uff0c\u4e5f\u5c31\u662f\u8ffd\u52a0\u5728URL\u4e0a\u7684\u952e\u503c\u5bf9\u3002\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\n@Controller\r\n@RequestMapping(path = &quot;\/xttblog&quot;)\r\npublic class XttblogController {\r\n    \/\/ \u8be5\u65b9\u6cd5\u5c06\u63a5\u6536 \/xttblog\/login \u53d1\u6765\u7684\u8bf7\u6c42\uff0c\u4e14\u8bf7\u6c42\u53c2\u6570\u5fc5\u987b\u4e3a username=xttblog&amp;password=123456\r\n    @RequestMapping(path = &quot;\/login&quot;, params={&quot;username=xttblog&quot;,&quot;password=123456&quot;})\r\n    public String login() {\r\n        return &quot;success&quot;;\r\n    }\r\n}<\/pre>\n<p>\n\t@RequestMapping \u7684 headers \u5c5e\u6027\uff0c\u8868\u793a\u8bf7\u6c42\u5934\u4e2d\u7684\u914d\u7f6e\u3002\u4f8b\u5982\u5e38\u89c1\u7684 token\uff0c\u4e00\u822c\u90fd\u662f\u653e\u5728\u8bf7\u6c42\u5934\u4e2d\u3002headers \u5c5e\u6027\u540c\u6837\u652f\u6301\u591a\u4e2a\u914d\u7f6e\u7684\u3002\n<\/p>\n<p>\n\t\u4f8b\u5982\uff0cheaders=&quot;Host=localhost:8080&quot; \u5219\u8868\u793a\u53ea\u63a5\u6536\u672c\u673a\u53d1\u6765\u7684\u8bf7\u6c42\u3002\n<\/p>\n<p>\n\t@RequestMapping \u4e2d\u7684 value \u5c5e\u6027\u8fd8\u652f\u6301\u5360\u4f4d\u7b26\u3002\u5982\u4e0b\u7528\u6cd5\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\n@RequestMapping(value=&quot;\/{id}&quot;, method=RequestMethod.GET)\r\npublic String show(@PathVariable(&quot;id&quot;) Integer id) {\r\n    return &quot;success&quot;;\r\n}<\/pre>\n<p>\n\tshow() \u65b9\u6cd5\u5c06\u53ef\u4ee5\u63a5\u6536 user\/1\u3001user\/2\u3001user\/3\u7b49\u8def\u5f84\u7684\u8bf7\u6c42\uff0c\u4f46\u662f\u8bf7\u6c42\u7684\u65b9\u6cd5\u5fc5\u987b\u4e3a GET\uff0c\u4f7f\u7528 @PathVariable \u4e3a\u5e94\u7528\u5b9e\u73b0 REST \u89c4\u8303\u63d0\u4f9b\u4e86\u5177\u5927\u7684\u4fbf\u5229\u6761\u4ef6\u3002\n<\/p>\n<p>\n\t@RequestMapping \u7684 value \u5c5e\u6027\u8fd8\u652f\u6301\u975e\u5e38\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4f46\u662f\u540c\u6837\u7684\u9700\u8981\u914d\u5408 @PathVariable \u6ce8\u89e3\u4e00\u8d77\u4f7f\u7528\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\n@RequestMapping(&quot;\/spring-web\/{symbolicName:[a-z-]+}-{version:\\d\\.\\d\\.\\d}.{extension:\\.[a-z]}&quot;)\r\npublic void handle(@PathVariable String version, @PathVariable String extension) {}<\/pre>\n<p>\n\tconsumes \u5c5e\u6027\u7528\u6765\u9650\u5236 ContentType\u3002ContentType \u7528\u6765\u544a\u8bc9\u670d\u52a1\u5668\u5f53\u524d\u53d1\u9001\u7684\u6570\u636e\u662f\u4ec0\u4e48\u683c\u5f0f\u3002<br \/>\n\tproduces \u7528\u6765\u9650\u5236 Accept\u3002Accept \u7528\u6765\u544a\u8bc9\u670d\u52a1\u5668\uff0c\u5ba2\u6237\u7aef\u80fd\u8ba4\u8bc6\u54ea\u4e9b\u683c\u5f0f\uff0c\u6700\u597d\u8fd4\u56de\u8fd9\u4e9b\u683c\u5f0f\u4e2d\u7684\u5176\u4e2d\u4e00\u79cd\u3002\n<\/p>\n<p>\n\t\u5982\u679c\u6211\u4eec\u914d\u7f6e\u4e86\uff0cconsumes={&quot;application\/xml&quot;,&quot;application\/json&quot;},produces={&quot;application\/xml&quot;} \u90a3\u4e48\u6211\u4eec\u5c31\u53ea\u80fd\u63a5\u6536 application\/xml \u683c\u5f0f\uff0c\u4e5f\u53ea\u8fd4\u56de xml \u683c\u5f0f\u3002\n<\/p>\n<p>\n\t\u81f3\u6b64\uff0c@RequestMapping \u7684\u5927\u591a\u7528\u6cd5\u90fd\u5df2\u7ecf\u89e3\u91ca\u5b8c\u4e86\u3002\u719f\u80fd\u751f\u5de7\uff0c\u5927\u5bb6\u90fd\u7528\u8d77\u6765\u5427\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3578\">\u8be6\u89e3 @RequestMapping \u4e2d\u7684 consumes \u548c produces<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>@RequestMapping \u6ce8\u89e3\u975e\u5e38\u7684\u5f3a\u5927\uff0cSpring MVC \u548c SpringBoot \u4e2d\u90fd\u4f1a\u7528\u5230\u8fd9\u4e2a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1364,1376,1384,1383,1374],"class_list":["post-3578","post","type-post","status-publish","format-standard","hentry","category-java","tag-requestmapping","tag-abstractcommandcontroller","tag-abstractformcontroller","tag-abstractwizardformcontroller","tag-basecommandcontroller"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3578"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3578\/revisions"}],"predecessor-version":[{"id":3579,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3578\/revisions\/3579"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}