标签:
被一个问题耽搁了好久,最后才恍然。这是关于HTTP status的。
使用feign进行http请求,结果总是抛出异常: read 405.由于不了解feign具体原理,还总觉得是内部错误。虽然错误信息没有明确指出http返回异常,但看到405就应该敏感才对。这里就记录遇到的各种status。
1.405 Method Not Allowed
请求方式不允许。即服务端只允许比如get,而你使用post获取则返回405.
The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.
restful url的含义就是资源定位,所以请求的都是resource。通过get,post,delete,option等来确定对应的行为。当请求为request的时候,服务端会返回一个response。这个response的header会告诉你他允许的行为:
Allow →GET Cache-Control →no-cache, no-store, max-age=0, must-revalidate Content-Type →application/json;charset=UTF-8 Date →Wed, 03 Aug 2016 12:52:52 GMT Expires →0 Pragma →no-cache Strict-Transport-Security →max-age=31536000 ; includeSubDomains Transfer-Encoding →chunked X-Content-Type-Options →nosniff X-Frame-Options →DENY X-XSS-Protection →1; mode=block
比如服务端:
@RequestMapping(value = "/map.json", method = RequestMethod.GET) @ResponseBody public Map map(){ Map map = new HashMap(); map.put("name","Ryan"); map.put("sex","man"); map.put("age",18); List list = new ArrayList(); list.add("red"); list.add("black"); list.add("blue"); list.add("yellow"); map.put("colors",list); return map; }
访问的request header为:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding:gzip, deflate, sdch Accept-Language:zh-CN,zh;q=0.8 Authorization:Basic YWRtaW46dGVzdA== Cache-Control:max-age=0 Connection:keep-alive Host:localhost:8080 Upgrade-Insecure-Requests:1 User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36
访问的request主题general为:
Request URL:http://localhost:8080/hello/map.json Request Method:GET Status Code:200 Remote Address:[::1]:8080
请求结果返回的response header为:
Cache-Control:no-cache, no-store, max-age=0, must-revalidate Content-Type:application/json;charset=UTF-8 Date:Wed, 03 Aug 2016 13:08:38 GMT Expires:0 Pragma:no-cache Strict-Transport-Security:max-age=31536000 ; includeSubDomains Transfer-Encoding:chunked X-Content-Type-Options:nosniff X-Frame-Options:DENY X-XSS-Protection:1; mode=block
显然没看到允许的行为是否是get,因为已经访问成功了。如果请求的行为不允许才会返回 Allow method.
新闻乐见。url访问的路径在服务端找不到的时候返回404.即服务端的所有路由中都不匹配你所请求的url。
The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.
标签:
原文地址:http://www.cnblogs.com/woshimrf/p/5734558.html