5

I am having a strange problem with ModelAttribute in Spring MVC 3.0. When I deploy the app at localhost, it works fine. But when I deploy the app on a remote server, it fails everytime user access a specific action, with the errors:

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)

The code that I user to init the ModelAttribute in that controller is:

    @ModelAttribute("id")
    public long getUserId(HttpSession session) {
        UserInfoHolder userHolder = (UserInfoHolder) session
                .getAttribute("userHolder");
        long userId = userHolder.getUserId();
        return userId;
    }

As far as I can tell, the bug can't be reproduced at my local workstation. And it happens before the action get called.

Looking in HandlerMethodInvoker.java (line 762), we see this line:

bindObject = BeanUtils.instantiateClass(paramType);

An experienced peer of mine believes that this line causes problem, because that a primitive-type ModelAttribute(long) doesn't have a constructor. I think that reason maybe right, but how can it explain that the web application works fine on my local server?

I tried to search to know if ModelAttribute support primitive data type, but with no good results. Does anyone have experience about this issue?

1 Answer 1

5

I'm just throwing in some thoughts, hoping it might help a bit.

The stack trace shows that it is indeed trying to call the constructor method for long:

java.lang.NoSuchMethodException: long.<init>()

Does it work if you change the used type from long to Long?

As for things working locally but not on the client. Is the Java version the same?

4
  • I tried it already, but it still fails with the same error for Long type. Java version is the same: Java 1.6 Jul 13, 2011 at 8:38
  • When you've done that, have you set a breakpoint at HandlerMethodInvoker.java (line 762) and checked what paramType is then? Did it get changed to Long too?
    – Raku
    Jul 13, 2011 at 8:52
  • 1
    Hmm, Long doesn't have a zero-arguments constructor either - which is what is called by BeanUtils. This looks more like a bug in Spring to me where they're failing to handle instantiation of primitive data types correctly. The only workaround I can see right now is using a wrapper class instead :-/.
    – Raku
    Jul 13, 2011 at 9:17
  • at the end I must accept the reality & make a work-around, too. Too bad since I haven't found even the reason that cause the bug Jul 13, 2011 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.