Announcement Announcement Module
Collapse
No announcement yet.
Jackson polymorphic deserialization mapped to spring controller method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Jackson polymorphic deserialization mapped to spring controller method

    I've got jackson deserialization able to determine which type I'm passing to it, so in my controller method I now need to test which one I've received and act on it accordingly.

    Is there a cleaner way of doing this?

    My DTO classes:
    Code:
    @RooJavaBean
    @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
    public class BaseDTO {...
    
    @RooJavaBean
    @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
    public class Type1-DTO extends BaseDTO {...
    
    @RooJavaBean
    @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
    public class Type2-DTO extends BaseDTO {...
    Base entity maps to a BaseDTO xfer object as do Type1 and Type1-DTO and Type2. The *DTO types only expose the entity attributes I want exposed via REST.
    The controller checks the xferObj type, instantiates the right entity type, then calls a converter, but it's calling the base class converter, not the subclass one.

    Then in my controller:
    Code:
        @RequestMapping(value = "/{id}/foo", method = RequestMethod.POST)
        @ResponseBody
        public BaseDTO createOffer(
                HttpServletRequest request,
                @Valid @RequestBody BaseDTO xferObject) {
        	BaseObj base = null;
        	if(xferObject instanceof Type1-DTO) {
        		offer = new Type1-DTO();
        	}
        	if(xferObject instanceof Type2-DTO) {
        		offer = new Type2-DTO();
        	}
            xferObject.toEntity(base); <<-- here, it's calling Base.toEntity, not Type1.toEntity or Type2.toEntity.
    So, question 1: Am I making this more complicated than it needs to be? IOW, what's the right way to handle polymorphic types coming in under one base type?
    Is there some clever trick for eliminating the manual type checking?

    question 2: Why is the toEntity() not picking up the subclass method and is calling the base class method instead?

    Thanks in advance.
Working...
X