{"id":1372,"date":"2025-01-23T18:30:54","date_gmt":"2025-01-23T10:30:54","guid":{"rendered":"https:\/\/blog.humh.cn\/?p=1372"},"modified":"2025-01-23T18:31:38","modified_gmt":"2025-01-23T10:31:38","slug":"1372","status":"publish","type":"post","link":"https:\/\/blog.humh.cn\/?p=1372","title":{"rendered":"Hutool \u4e4b \u201cInvalid HTTP method: PATCH\u201d"},"content":{"rendered":"\n<p>\u5f53\u524d Hutool 5.8.1 \u53ef\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u53bb\u53d1\u8d77 Patch \u8bf7\u6c42<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import cn.hutool.http.*;\n\nHttpRequest request = HttpUtil.createRequest(Method.PATCH, url);\nrequest.body(pathBody);\nHttpResponse response = request.execute();<\/pre>\n\n\n\n<p>\u4f46\u5b9e\u9645\u8bf7\u6c42\u4f1a\u5076\u73b0\u5982\u4e0b\u9519\u8bef<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Caused by: cn.hutool.http.HttpException: Invalid HTTP method: PATCH\n\tat cn.hutool.http.HttpConnection.setMethod(HttpConnection.java:131) ~[hutool-all-5.8.1.jar!\/:na]\n\tat cn.hutool.http.HttpRequest.initConnection(HttpRequest.java:1158) ~[hutool-all-5.8.1.jar!\/:na]\n\tat cn.hutool.http.HttpRequest.doExecute(HttpRequest.java:1121) ~[hutool-all-5.8.1.jar!\/:na]\n\tat cn.hutool.http.HttpRequest.execute(HttpRequest.java:1012) ~[hutool-all-5.8.1.jar!\/:na]\n\tat cn.hutool.http.HttpRequest.execute(HttpRequest.java:988) ~[hutool-all-5.8.1.jar!\/:na]\nCaused by: java.net.ProtocolException: Invalid HTTP method: PATCH\n\tat java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:440) ~[na:1.8.0_111]\n\tat sun.net.www.protocol.http.HttpURLConnection.setRequestMethod(HttpURLConnection.java:552) ~[na:1.8.0_111]<\/pre>\n\n\n\n<h2>\u539f\u56e0\u5206\u6790<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img width=\"1024\" height=\"761\" class=\"wp-image-1374 lazyload\" title=\"Hutool \u4e4b \u201cInvalid HTTP method: PATCH\u201d\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2025\/01\/20250123171823321-1024x761.png\" alt=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2025\/01\/20250123171823321-1024x761.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2025\/01\/20250123171823321-1024x761.png 1024w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2025\/01\/20250123171823321-300x223.png 300w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2025\/01\/20250123171823321-768x570.png 768w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2025\/01\/20250123171823321-1536x1141.png 1536w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2025\/01\/20250123171823321.png 1586w\" sizes=\"(max-width: 1024px) 100vw, 1024px\"><\/figure>\n\n\n\n<p>\u5982\u4e0a\u56fe\u6240\u793a\uff0cHutool \u5728\u8bbe\u7f6e\u8bf7\u6c42\u7c7b\u578b\u4e3a Patch \u65f6\u4f1a\u8c03\u7528 HttpURLConnection#setRequestMethod \u65b9\u6cd5\uff08\u5982\u4e0b\u56fe\u6240\u793a\uff09\uff0c\u800c\u8be5\u65b9\u6cd5\u5185\u90e8\u5e76\u4e0d\u652f\u6301 Patch\u3002\u82e5\u6267\u884c Patch \u8bf7\u6c42\uff0c\u5219\u4f1a\u629b\u51fa\u4e0a\u6587\u63d0\u5230\u7684\u5f02\u5e38\u3002\u4f46 Hutool \u8fd9\u91cc\u901a\u8fc7\u53cd\u5c04\u4fee\u6539 <meta charset=\"utf-8\">HttpURLConnection.methods \u9759\u6001\u53d8\u91cf\u503c\u5c06\u201cPATCH\u201d\u52a0\u5165\u5176\u4e2d\uff0c\u4ee5\u6b64<a href=\"https:\/\/stackoverflow.com\/questions\/25163131\/httpurlconnection-invalid-http-method-patch\" target=\"_blank\" rel=\"noreferrer noopener\">\u5c1d\u8bd5\u89e3\u51b3\u8be5\u95ee\u9898<\/a>\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img width=\"1024\" height=\"587\" class=\"wp-image-1373 lazyload\" title=\"Hutool \u4e4b \u201cInvalid HTTP method: PATCH\u201d\" src=\"data:image\/gif;base64,R0lGODlhAgABAIAAALGxsQAAACH5BAAAAAAALAAAAAACAAEAAAICBAoAOw==\" data-src=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2025\/01\/20250123171226962-1024x587.png\" alt=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2025\/01\/20250123171226962-1024x587.png\" data-srcset=\"https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2025\/01\/20250123171226962-1024x587.png 1024w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2025\/01\/20250123171226962-300x172.png 300w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2025\/01\/20250123171226962-768x440.png 768w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2025\/01\/20250123171226962-1536x880.png 1536w, https:\/\/file.blog.humh.cn\/wp-content\/uploads\/2025\/01\/20250123171226962-2048x1174.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\"><\/figure>\n\n\n\n<p>\u4f46\u8fd9\u79cd\u65b9\u5f0f\u5076\u73b0\u5931\u8d25\uff0c\u539f\u56e0\u672a\u77e5 <a href=\"https:\/\/gitee.com\/dromara\/hutool\/issues\/I3Z3DH\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/gitee.com\/dromara\/hutool\/issues\/I3Z3DH<\/a><\/p>\n\n\n\n<h2>\u89e3\u51b3<\/h2>\n\n\n\n<p>\u53ef\u901a\u8fc7 HttpClient \u6216\u5176\u4ed6\u5de5\u5177\u5b8c\u6210 Patch \u8bf7\u6c42\uff0c\u5982\u4e0b\u6211\u4eec\u5728 SpringBoot \u9879\u76ee\u4e2d\u4f7f\u7528 RestTemplate \u66ff\u4ee3 Hutool HttpUtil<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import org.springframework.http.*;\nimport org.springframework.web.client.RestTemplate;\n\nHttpHeaders headers = new HttpHeaders();\nheaders.setContentType(MediaType.APPLICATION_JSON);\nHttpEntity&lt;String> entity = new HttpEntity&lt;>(pathBody, headers);\nResponseEntity&lt;JSONObject> responseEntity = restTemplate.exchange(url, HttpMethod.PATCH, entity, JSONObject.class);<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>\u672c\u6587\u53c2\u8003<\/strong><\/p>\n\n\n\n<ul><li>\u201cHttpURLConnection Invalid HTTP method: PATCH\u201d\uff1ahttps:\/\/stackoverflow.com\/questions\/25163131\/httpurlconnection-invalid-http-method-patch<\/li><li>\u201cInvalid HTTP method: PATCH\u201d\uff1ahttps:\/\/gitee.com\/dromara\/hutool\/issues\/I3Z3DH<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d Hutool 5.8.1 \u53ef\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u53bb\u53d1\u8d77 Patch \u8bf7\u6c42 \u4f46\u5b9e\u9645\u8bf7\u6c42\u4f1a\u5076\u73b0\u5982\u4e0b\u9519\u8bef Caused [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[51],"tags":[35],"_links":{"self":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/1372"}],"collection":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1372"}],"version-history":[{"count":3,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/1372\/revisions"}],"predecessor-version":[{"id":1377,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=\/wp\/v2\/posts\/1372\/revisions\/1377"}],"wp:attachment":[{"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.humh.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}