Dropwizard ExceptionMapper:验证原始标题字段

时间:2014-10-09 15:28:56

标签: dropwizard

有谁知道如何从请求中获取原始标头字段?我想验证,如果客户端将收到HTML或只是普通/文本响应。我可以将这些字段放入' toResponse' exceptionMapper的方法?

我在这篇文章中创建了exceptionMapper: http://gary-rowe.com/agilestack/2012/10/23/how-to-implement-a-runtimeexceptionmapper-for-dropwizard/

1 个答案:

答案 0 :(得分:4)

如果您确实想从原始请求对象获取信息,可以将以下内容添加到控制器中。

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());
  }