vpiTriumph
vpiTriumph

Reputation: 3166

Specify type for @RequestBody when using an abstract class

I have an endpoint that I have receiving an abstract class:

@RequestMapping(value = URL, method = RequestMethod.POST)
@ResponseBody
public ModelMap update(@RequestBody SomeAbstractClass form) {
      ...
}

When I post to it I get the error:

Could not read JSON: Can not construct instance of ..., 
problem: abstract types can only be instantiated with dditional type information

I understand why I'm getting this error, but I can't figure out where I would specify the additional type information. Ideally I could specify the type of the concrete class through one of the annotations, but any solution would suffice at this point.

Upvotes: 1

Views: 2124

Answers (1)

danny.lesnik
danny.lesnik

Reputation: 18639

I believe you are uisng Jackson as JSON serailizer.

First of all make sure that you have

 <context:annotation-config/> 

in your spring xml conf file.

I used to have the same issue De-serialization of Inherited classes I solved it by annotating it

@JsonDeserialize(as=Child.class)
public abstract class AbstractParent { ... }

Upvotes: 2

Related Questions