gpt4 book ai didi

java - HandlerInterceptorAdapter 似乎没有转发 HttpServletRequestWrapper

转载 作者:行者123 更新时间:2023-11-29 03:43:57 25 4
gpt4 key购买 nike

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

知道这个上下文考虑这个

@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;
}

这是拦截器之前我的 Controller 方法,下面是拦截器

@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);
}

列表中的下一个是请求包装器

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(...)。我希望然后进入 Controller 并愉快地进一步使用我的 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);
}

关于java - HandlerInterceptorAdapter 似乎没有转发 HttpServletRequestWrapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11890516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com