被一个问题耽搁了好久,最后才恍然。这是关于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; }