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