1

我正在尝试为我的 JSON 网络服务创建一个验证系统。这个想法是让一个拦截器捕获每个 Json @RequestBody,通过JacksonMapping. 然后ValidationAnnotations从中读取以检查数据,当通过完成常规流程时,将使用包含错误消息的模型和视图图发送回发送者。

知道这个上下文考虑这个

@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?

4

1 回答 1

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);
}
于 2013-03-11T11:16:31.940 回答