簡體   English   中英

HandlerInterceptorAdapter似乎沒有轉發HttpServletRequestWrapper

[英]HandlerInterceptorAdapter doesn't seem to forward HttpServletRequestWrapper

我正在嘗試為我的JSON Web服務創建驗證系統。 我們的想法是讓一個攔截器捕獲每個Json @RequestBody ,通過JacksonMapping創建一個JSON對象。 然后從中讀取ValidationAnnotations以檢查數據,並在通過完成常規流時,通過包含錯誤消息的modelandviewmap將其發送回發送方。

知道這個背景考慮這一點

@RequestMapping(value="/rest/contact/list/filter", method = RequestMethod.POST, consumes="application/json", produces="application/json")
public @ResponseBody JsonContactList findFilteredContacts(@RequestBody JsonContactSelectorData selectorData, Model model) throws Exception {
    MbaLog.debugLog(logger,"Finding Contacts with selector data: " + selectorData);     
    JsonContactList result = contactService.findContacts(selectorData);
    return result;
}

這是攔截器之前的控制器方法,以下是攔截器

@Override
public boolean preHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler) throws Exception {
    ValidationRequestWrapper wrapper = new ValidationRequestWrapper(request);
    //do complex validation here 
    return super.preHandle(wrapper, response, handler);
}

列表中的下一個是requestwrapper

public class ValidationRequestWrapper extends HttpServletRequestWrapper {

    private String jsonString;

    public ValidationRequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        jsonString = "";
        Scanner scanner = new Scanner(request.getInputStream(), "UTF-8").useDelimiter("\\A");
        if (scanner.hasNext())
            jsonString = scanner.next();

        System.out.println("need a break");
    }

    @Override  
    public ServletInputStream getInputStream ()   
        throws IOException {   

        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(jsonString.getBytes());   
        ServletInputStream inputStream = new ServletInputStream() {   
            public int read ()    
                throws IOException {   
                return byteArrayInputStream.read();   
            }   
        };   
        return inputStream;   
    }   
}

正如您所看到的,我抓取請求輸入流,然后覆蓋getInputStream()就像在這些情況下自定義一樣。 在攔截器中,你可以看到我實例化這個包裝器並將其傳遞給super.prehandle(...)。 我希望然后進入控制器並與我的jsonobject一起愉快地工作。 然而結果卻相當令人失望。

java.io.EOFException: No content to map to Object due to end of input

似乎沒有調用重寫的輸入流方法? 或者我是否完全錯誤地使用了這個機制? 我甚至可以使用HandlerInterceptorAdapter嗎?

我設法弄清楚HandlerInterceptorAdapter無法在我的問題中執行我所要求的功能。

解決這個問題的唯一方法是使用javax.servlet.Filter

@Override
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
    InterceptorRequestWrapper myRequestWrapper = new InterceptorRequestWrapper((HttpServletRequest) request);
    String httpRequestMethod = ((HttpServletRequest) request).getMethod();
    if("POST".equals(httpRequestMethod) || "PUT".equals(httpRequestMethod)){
        String body = myRequestWrapper.getBody();
        ErrorObject errorObject = new ErrorObject();
        errorObject.setSource(body);
        Map<String, List<ErrorMessage>> errorMessages = new HashMap<String, List<ErrorMessage>>();
        ErrorMessage error = new ErrorMessage();
        error.setErrorLabel("myerror");
        errorMessages.put("test", Arrays.asList(error));
        errorObject.setErrorMessages(errorMessages);
        myRequestWrapper.setAttribute("errorObject", errorObject);
    }
    chain.doFilter(myRequestWrapper, response);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM