我正在尝试在我的spring mvc项目中添加自定义解析器。解析器代码在这里:
public class CurrentUserWebArgumentResolver implements WebArgumentResolver {
@Override
public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception {
if (methodParameter.getParameterType().equals(User.class)) {
User host = (User)webRequest.getAttribute(WebConstant.HTTP_REQUEST_HOST_NAME, RequestAttributes.SCOPE_REQUEST);
return host;
}
return null;
}
}
我更改了默认注释spring config:
<mvc:annotation-driven >
<mvc:argument-resolvers>
<bean class="com.xxx.web.resolver.CurrentUserWebArgumentResolver" />
</mvc:argument-resolvers>
</mvc:annotation-driven>
我的控制器功能在这里:
@RequestMapping(value = "register", method = RequestMethod.POST)
public ModelAndView store(@Valid RegisterForm registerForm, BindingResult result) {
ModelAndView mv = new ModelAndView("user/register");
if (result.hasErrors()) {
return mv;
}
User user = registerForm.getUser();
if (registerService.register(user)) {
mv.setView(new RedirectView("/"));
}
return mv;
}
现在,当我请求此网址时,我收到此错误:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: An Errors/BindingResult argument is expected to be declared immediately after the model attribute, the @RequestBody or the @RequestPart arguments to which they apply: public org.springframework.web.servlet.ModelAndView com.xxx.web.controller.user.RegisterController.store(com.xxx.web.validate.form.user.RegisterForm,org.springframework.validation.BindingResult)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:822)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.IllegalStateException: An Errors/BindingResult argument is expected to be declared immediately after the model attribute, the @RequestBody or the @RequestPart arguments to which they apply: public org.springframework.web.servlet.ModelAndView com.xxx.web.controller.user.RegisterController.store(com.xxx.web.validate.form.user.RegisterForm,org.springframework.validation.BindingResult)
org.springframework.web.method.annotation.ErrorsMethodArgumentResolver.resolveArgument(ErrorsMethodArgumentResolver.java:62)
org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:822)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
我认为必须有一些其他解析器我没有添加到spring配置文件
请帮助我谢谢
答案 0 :(得分:2)
您违反了contract
返回:参数值,如果不可解析则返回UNRESOLVED
您正在返回违反合同的null
。替换为UNRESOLVED
静态变量。
public class CurrentUserWebArgumentResolver implements WebArgumentResolver {
@Override
public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception {
if (methodParameter.getParameterType().equals(User.class)) {
User host = (User)webRequest.getAttribute(WebConstant.HTTP_REQUEST_HOST_NAME, RequestAttributes.SCOPE_REQUEST);
return host;
}
return UNRESOLVED;
}
}