1

I can't make spring return a serialization of my object with the additional property defining the class.

My classes are:

@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{
...
}

In my unit test, an Study instance is serialised properly, having the extra property for the class:

{"ObjectType":"Study",
...
}

Using for this a simple:

ObjectMapper mapper = new ObjectMapper();
mapper.readValue(studyJSON,study.getClass());

However, in my Spring Rest webservice module the study is serialized without the "ObjectType" property.

The controller looks like this (simplified):

@ResponseBody
public RestResponse<Study> getStudyById(@PathVariable("studyIdentifier")    String studyIdentifier) throws DAOException {

    return getStudyRestResponse(studyIdentifier);
}

EDIT: Adding RestResponse (Simplified)

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;
}

Any idea why spring seems to be ignoring the @JsonType annotations?

Pablo
  • 203
  • 2
  • 8
  • Have you tried searching stackoverflow? Perhaps some of these can help you: [Spring \@ResponseBody Jackson JsonSerializer with JodaTime](http://stackoverflow.com/questions/10649356/spring-responsebody-jackson-jsonserializer-with-jodatime), [Spring configure \@ResponseBody JSON format](http://stackoverflow.com/questions/4823358/spring-configure-responsebody-json-format)... – maricn Jul 24 '15 at 08:29
  • 1
    Thanks maricn...I don't have a custom ObjectMapper..and I wouldn't like to have one, if possible!. ;-) And yes I have searched stackoverflow and haven't found any answer to this question. Am I missing anything? – Pablo Jul 24 '15 at 09:26
  • what is this class RestResponse? – Nikolay Rusev Jul 24 '15 at 10:11
  • RestResponse is a Wrapper class, i'll paste it. It will have a getContent() that returns the Study. – Pablo Jul 24 '15 at 11:27

1 Answers1

2

Try to return only the object you need, do not wrap it in generic wrapper class. Your problem is related to Java Type erasure. See more info here

@ResponseBody
    public @ResponseBody Study getStudyById(@PathVariable("studyIdentifier")    String studyIdentifier) throws DAOException {

        return studyIdentifier;
    }
Nikolay Rusev
  • 4,060
  • 3
  • 19
  • 29
  • That was it!. Since I want to keep the RestResponse wrapper I created a class extending RestResponse like this: `public class StudyRestResponse extends RestResponse { }`. This way I can keep the wrapper and, I guess, type erasure does not happen anymore. Many thanks. – Pablo Jul 24 '15 at 12:53