Java Spring 3中的ResponseBy忽略@JsonTypeInfo

Java Spring 3中的ResponseBy忽略@JsonTypeInfo,java,rest,spring-mvc,inheritance,jackson,Java,Rest,Spring Mvc,Inheritance,Jackson,我无法使用定义类的附加属性使spring返回对象的序列化 我的课程是: @JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include= JsonTypeInfo.As.PROPERTY, property="ObjectType") @JsonSubTypes({ @JsonSubTypes.Type(value=LiteStudy.class, name="LiteStudy") }) public class Entity { ... } @Js

我无法使用定义类的附加属性使spring返回对象的序列化

我的课程是:

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include= JsonTypeInfo.As.PROPERTY, property="ObjectType")
@JsonSubTypes({
    @JsonSubTypes.Type(value=LiteStudy.class, name="LiteStudy")
})
public class Entity {
...
}


@JsonTypeName("LiteStudy")
@JsonSubTypes({
        @JsonSubTypes.Type(value=Study.class, name="Study")
})
public class LiteStudy extends Entity {
...
}


@JsonTypeName("Study")
public class Study extends LiteStudy{
...
}
在我的单元测试中,一个研究实例被正确序列化,具有该类的额外属性:

{"ObjectType":"Study",
...
}
为此,请使用一个简单的:

ObjectMapper mapper = new ObjectMapper();
mapper.readValue(studyJSON,study.getClass());
但是,在我的SpringRESTWebService模块中,研究是序列化的,没有“ObjectType”属性

控制器如下所示(简化):

@ResponseBody
public Response getStudyById(@PathVariable(“studyIdentifier”)字符串studyIdentifier)引发异常{
返回GetStudyResponse(studyIdentifier);
}
编辑:添加重响应(简化)

公共类重新响应{
私有内容;
私有字符串消息;
私人例外错误;
公共内容getContent(){
返回内容;
}
公共内容(内容){
this.content=内容;
}
公共字符串getMessage(){
返回消息;
}
公共无效设置消息(字符串消息){
this.message=消息;
}
公共异常getErr(){
返回错误;
}
公共无效设置错误(异常错误){
this.err=err;
}

知道为什么spring似乎忽略了@JsonType注释吗?

尝试只返回您需要的对象,不要将其包装在通用包装类中。您的问题与Java类型擦除有关。请参阅更多信息


你尝试过搜索stackoverflow吗?也许其中一些可以帮助你:,…谢谢maricn…我没有自定义的ObjectMapper..如果可能的话,我也不想有一个!-)是的,我搜索过stackoverflow,但没有找到这个问题的任何答案。我遗漏了什么吗?这个类是什么?RestResponse?RestResponse是一个包装器类,我将粘贴它。它将有一个返回研究的getContent()。就是这样!因为我想保留RestResponse包装,所以我创建了一个扩展RestResponse的类,如下所示:
public class studyrresponse extensed RestResponse{}
。这样我就可以保留包装器,而且我想,不会再发生类型擦除。非常感谢。
@ResponseBody
public RestResponse<Study> getStudyById(@PathVariable("studyIdentifier")    String studyIdentifier) throws DAOException {

    return getStudyRestResponse(studyIdentifier);
}
public class RestResponse<Content> {

private Content content;
private String message;
private Exception err;

public Content getContent() {
    return content;
}

public void setContent(Content content) {
    this.content = content;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public Exception getErr() {
    return err;
}

public void setErr(Exception err) {
    this.err = err;
}
@ResponseBody
    public @ResponseBody Study getStudyById(@PathVariable("studyIdentifier")    String studyIdentifier) throws DAOException {

        return studyIdentifier;
    }