JSON继承不适用于带有包装对象的扩展pojo类

时间:2018-10-25 09:18:17

标签: java json spring jackson swagger

在这种情况下,我有两个pojo类(一个正在扩展另一个)。 我的父级pojo类请求具有不包含wrapperobject的jsoninclude,并且当我通过wrapperobject传递时,我的孩子应该工作相同。

我最大的担心是我的孩子pojo本身没有被调用,请帮助我

//sample parent pojo class with json properties
@JsonInclude(Include.NON_EMPTY)
@XmlRootElement(name = "employee")
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@JsonRootName("employee")
@JsonTypeName("employee")
//and tired included this also in employee class
//tired with annotations also
//@JsonTypeInfo(include = As.WRAPPER_OBJECT, use = Id.NAME)
//@JsonSubTypes(value = {@Type(value = EmployeeV2.class)})
public class Employee{
private empid;
private empname;
//with getters and setters
//to register the subtypes into the parent pojo
static
    {
        JSON_MAPPER.registerSubtypes(EmployeeV2.class);
    }

}

Now i have extended the same POJO as
//my child pojo class
@ApiVersion("2.2")
@JsonTypeName("employee")
@JsonInclude(Include.ALWAYS) 
Class EmployeeV2 extends Employee{
}

My new version API is not getting invoked when i pass my request with **wrapper object**
//request with wrapper object
{
"employee":
{
 "empid": "121",
 "empname": "TestDownloadJob1"
}
}

working fine without the wrapper object,request without wrapperobject
{
 "empid": "121",
 "empname": "TestDownloadJob1"
}

at the controller layer added the **@ExposesResourceFor(EmployeeV2.class)** where my class can accept array of resources.

0 个答案:

没有答案