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

  • SEARCH
  • Home
  • 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 6558743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:14:14+00:00 2026-05-25T13:14:14+00:00

I have a hierarchy of model classes in my Spring/Hibernate application. When submitting a

  • 0

I have a hierarchy of model classes in my Spring/Hibernate application.

When submitting a POST form to a Spring MVC controller, is there any standard way of specifying the type of the object being submitted, so Spring can instantiate the correct subclass of the type declared in the receiving method’s @ModelAttribute or @RequestParam?

For example:

public abstract class Product {...}
public class Album extends Product {...}
public class Single extends Product {...}


//Meanwhile, in the controller...
@RequestMapping("/submit.html")
public ModelAndView addProduct(@ModelAttribute("product") @Valid Product product, BindingResult bindingResult, Model model)
{
...//Do stuff, and get either an Album or Single
}

Jackson can deserialize JSON as a specific subtype using the @JsonTypeInfo annotation. I’m hoping Spring can do the same.

  • 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-25T13:14:15+00:00Added an answer on May 25, 2026 at 1:14 pm

    Jackson can deserialize JSON as a specific subtype using the
    @JsonTypeInfo annotation. I’m hoping Spring can do the same.

    Assuming you use Jackson for type conversion (Spring uses Jackson automatically if it finds it on the classpath and you have <mvc:annotation-driven/> in your XML), then it has nothing to do with Spring. Annotate the types, and Jackson will instantiate the correct classes. Nevertheless, you will have to do instanceof checks in your Spring MVC controller method.

    Update after comments:

    Have a look at 15.3.2.12 Customizing WebDataBinder initialization. You could use an @InitBinder method that registers an editor based on a request parameter:

    @InitBinder
    public void initBinder(WebDataBinder binder, HttpServletRequest request) {
        String productType = request.getParam("type");
    
        PropertyEditor productEditor;
        if("album".equalsIgnoreCase(productType)) {
            productEditor = new AlbumEditor();
        } else if("album".equalsIgnoreCase(productType))
            productEditor = new SingleEditor();
        } else {
            throw SomeNastyException();
        }
        binder.registerCustomEditor(Product.class, productEditor);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been organizing my test cases by a hierarchy of controller/action/scenario or model/method/scenario.
I have an hierarchy of classes that looks like the following: class Critical {
I have an hierarchy of classes that I fetch from database by LINQ to
I have a hierarchy of classes that are serialised to XML using XMLSerialiser .
Working with the MVVM pattern, I have a pair of view model classes that
In C#, I have a class hierarchy with a couple of abstract base classes
I have a hierarchy of classes that all derive from a base type and
I have a Rails app with a few model classes (e.g. Category , Subcategory
I am working on my model hierarchy and have a few questions. 1) I
I have the following hierarchy of classes: /** * @Entity * @InheritanceType(SINGLE_TABLE) * @DiscriminatorColumn(name=discr,

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.