处理Http HEAD请求和标题内容 - 春天3.1 mvc

时间:2013-12-05 13:56:46

标签: java spring http spring-mvc http-headers

我收到了很多405错误,因为我还没有支持HEAD请求。现在,当我尝试解决这个问题时,我想到了以下问题:

a)是否有更简单的方法来支持GAD-URL /资源的HEAD? http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

    @RequestMapping(value = "/**/p/{productCode}", method = RequestMethod.GET)
    public String productDetail(@PathVariable("productCode"), final Model model, final HttpServletRequest request) {

          // some stuff...          

          return ControllerConstants.GET_PRODUCT_DETAIL;
    }

或者我必须为每个方法设置映射吗?

@RequestMapping(value = "/**/p/{productCode}", method = RequestMethod.HEAD)
public String productDetailHead() {
      final HttpHeaders headers = new HttpHeaders();
      return new ResponseEntity(null, headers, HttpStatus.OK);
}

b)应支持HEAD的哪些HTTP头属性? (经验法则?)

我的实际回应是: curl -I http://localhost:9001

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: _seaT.tenantID_=""; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/
Set-Cookie: JSESSIONID=D07B464BBA02DC4148F00C5A08421B51; Path=/
Content-Length: 0
Date: Thu, 05 Dec 2013 13:41:42 GMT

其他信息: 网上商店,STS 3.1,JDK 6,JSR 2.5

感谢。

2 个答案:

答案 0 :(得分:3)

回答你(b)的问题。根据经验,在HEAD方法调用期间也应传递在GET方法期间传递的所有头属性/指令,这就是HTTP标准所说的。

The HEAD method is identical to GET except that the server MUST NOT
return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.  

答案 1 :(得分:2)

同样的问题在这里:Spring 3.0 HEAD Requests
不久请参考:http://axelfontaine.com/blog/http-head.html