2016-06-25 4 views
0

다음은 Feed.type에 따라 어떤 유형이 될 수 내 수업변환 중첩 된 필드는

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

Feed.object입니다 오브젝트. 그러나 클래스의 문서를 elasticsearch으로 업로드 할 때 모든 것이 올바르게 작동하지만 문서를 다시 가져 오면 org.codehaus.jackson.map.ObjectMapperFeed.objectLinkedHashMap으로 변환합니다. 실제 객체를 가져올 수있는 방법이 있습니까? JSON 문자열은 피드에 포함됩니다.

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

'@ JsonTypeInfo'. –

답변

1

당신은 Feed 클래스 type의 값에 따라 objectclass 무엇인지 나타 내기 위해 @JsonTypeInfo를 사용할 수 있습니다

다음

는 변환입니다. 예 :

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; 
} 
관련 문제