HandlerInterceptorAdapter似乎没有转发HttpServletRequestWrapper

时间:2012-08-09 19:22:48

标签: java spring servlets spring-mvc interceptor

我正在尝试为我的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

似乎没有调用overriden inputstream方法?或者我是否完全错误地使用了这个机制?我甚至可以使用HandlerInterceptorAdapter吗?

1 个答案:

答案 0 :(得分:4)

我设法弄清楚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);
}