1

Following is my class

class Feed {
    Long id;
    String title;
    String text;
    Short type;
    Object object;
}

Feed.object can be of any type based on Feed.type. When I upload the document of the class into elasticsearch every thing works fine, however, when the document is fetched back then org.codehaus.jackson.map.ObjectMapper converts Feed.object in LinkedHashMap. Is there a way to get the actual object? The JSON string I get is of Feed.

Following is the conversion:

Feed feed = mapper.readValue(response.getHits().getHits()[0].getSourceAsString(), Feed.class);
0

1 Answer 1

1

You can use @JsonTypeInfo to indicate what is the class of object according to the value of type in Feed class. For example,

class Feed {
    Long id;
    String title;
    String text;
    Short type;
    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_POPERTY, propery = "type")
    @JsonSubTypes({
        @JsonSubTypes.Type(value = Foo.class, name = "1"),
        @JsonSubTypes.Type(value = Bar.class, name = "2")
    })
    Object object;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.