我在 Spring MVC 3.0 中对 ModelAttribute 有一个奇怪的问题。当我在 localhost 部署应用程序时,它工作正常。但是当我在远程服务器上部署应用程序时,每次用户访问特定操作时它都会失败,并出现错误:
ERROR: my.package.application.web.filter.ExceptionFilter - long.<init>()
java.lang.NoSuchMethodException: long.<init>()
at java.lang.Class.getConstructor0(Class.java:2706)
at java.lang.Class.getDeclaredConstructor(Class.java:1985)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:762)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:356)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:153)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
我用于在该控制器中初始化 ModelAttribute 的代码是:
@ModelAttribute("id")
public long getUserId(HttpSession session) {
UserInfoHolder userHolder = (UserInfoHolder) session
.getAttribute("userHolder");
long userId = userHolder.getUserId();
return userId;
}
据我所知,该错误无法在我的本地工作站上重现。它发生在动作被调用之前。
查看 HandlerMethodInvoker.java(第 762 行),我们看到这一行:
bindObject = BeanUtils.instantiateClass(paramType);
我的一位经验丰富的同行认为这条线会导致问题,因为原始类型 ModelAttribute(long) 没有构造函数。我认为这个原因可能是正确的,但它如何解释 Web 应用程序在我的本地服务器上运行良好?
我试图搜索以了解 ModelAttribute 是否支持原始数据类型,但没有好的结果。有没有人有这个问题的经验?