2014-10-09 67 views
3

有谁知道如何从请求中获取原始标题字段?我想验证,如果客户端将收到HTML或只是普通/文字的回应。我可以在'toResponse'的exceptionMapper方法中获得这个字段吗?Dropwizard ExceptionMapper:验证原始标题字段

我创建exceptionMapper喜欢在这个帖子: http://gary-rowe.com/agilestack/2012/10/23/how-to-implement-a-runtimeexceptionmapper-for-dropwizard/

+0

我结束了创建过滤器 - > https://dropwizard.github.io/dropwizard/manual/core.html#jersey-filters – user3280180 2014-10-15 13:48:00

回答

3

如果你没有想从原始请求对象的信息,您可以添加以下到您的控制器。

import javax.servlet.http.HttpServletRequest; 
import javax.ws.rs.core.Context; 

@Path("/my") 
@Produces(["application/json", "application/hal+json"]) 
class MyController { 

    @Context 
    protected HttpServletRequest httpRequest 

    @Timed 
    @GET 
    public Response getOne(){ 
    httpRequest.getHeaders(); 
    ... //do something with headers 
    return Response.ok(new Person(id:1), httpRequest.getContentType()); 
    } 
+1

啊,这也工作在异常映射。这很有趣,因为我认为我已经测试过了...... – user3280180 2014-10-21 07:11:09