2014-10-09 32 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