Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6924125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:37:23+00:00 2026-05-27T10:37:23+00:00

I’m trying to use a generic class as a Spring form backing bean, but

  • 0

I’m trying to use a generic class as a Spring form backing bean, but end up with a ClassCastException when the Spring framework attempts to cast the Object into the actual type.

On submission of the form, the following error occurs when attempting to call a method on the SrvRecord object (line 105, marked with comment):

java.lang.ClassCastException: java.lang.Object cannot be cast to com.[...].portal.entity.SrvRecord
        at com.[...].portal.controller.SrvController.add(SrvController.java:105)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
        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:669)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:585)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
        at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
        at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
        at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
        at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
        at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
        at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:440)
        at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
        at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
        at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
        at org.mortbay.jetty.Server.handle(Server.java:326)
        at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
        at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:943)
        at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
        at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
        at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
        at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
        at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

Form Bean:

public class RecordBean<T>
{

    private T original;
    private T modified;

    public RecordBean()
    {
        super();
    }

    public RecordBean(T original)
    {
        this.original = original;
        this.modified = original;
    }

    public T getOriginal()
    {
        return original;
    }

    public void setOriginal(T original)
    {
        this.original = original;
    }

    public T getModified()
    {
        return modified;
    }

    public void setModified(T modified)
    {
        this.modified = modified;
    }

}

Controller methods:

@RequestMapping(value = "new", method = RequestMethod.GET)
public String add(Model model)
{
    SrvRecord srvRecord = getSrvRecord();

    RecordBean<SrvRecord> record = new RecordBean<SrvRecord>(srvRecord);
    model.addAttribute("record", record);

    return "generic/new";
}

@RequestMapping(value = "new", method = RequestMethod.POST)
public String add(Model model, @ModelAttribute("record") RecordBean<SrvRecord> record)
{
    // Call a method on the SrvRecord object
    doSomething(record.getModified().getZone().getName());  // line 105
    doSomething(record.getOriginal().getZone().getName());

    // ...
}

View:

<c:url value="/edit" var="formUrl" />
<form:form commandName="record" action="${formUrl}">
    <form:input type="hidden" path="original.zone" />
    <form:input type="hidden" path="original.name" />    
    <!-- ... -->

    <form:input path="modified.zone" /><br />
    <form:input path="modified.name" /><br />
    <!-- ... -->
</form:form>

Any thoughts or suggestions would be great. Being able to work with the generic form bean will eliminate a large amount of unnecessary code from the baseline.

Just for reference, the Spring version being used is 3.0.6.RELEASE.

Thanks,
Beau

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:37:24+00:00Added an answer on May 27, 2026 at 10:37 am

    You can file a bug at Spring. The problem is that Spring is using reflection to determine the parameter types and they are ignoring the generics therefore just instantiating a plain RecordBean object without the generic. As a result the objects inside RecordBean are just created as Object and there is no way to cast it to SrvRecord. The only workaround is to not to use generics.

    BACKGROUND

    Spring internally uses the MethodParameter class to read out the method parameters. There is a method called

    public Class<?> getParameterType()
    

    that calls :

    this.method.getParameterTypes()[this.parameterIndex]
    

    this code reads out the parameters without generics. They would need to call this to handle it properly

    this.method.getGenericParameterTypes()[this.parameterIndex]
    

    Later the method

     HandlerMethodInvoker.resolveModelAttribute()
    

    is called to instantiate the command class by doing

     bindObject = BeanUtils.instantiateClass(paramType);
    

    but the value of paramType is “com.test.RecordBean” and not “com.test.RecordBean<SrvRecord>” as it would be expected

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.