多态反序列化杰克逊问题
Polymorphic deserialization Jackson issue
Java :
/**
*/
@JsonTypeInfo(include=As.PROPERTY, property="_type", use=Id.NAME)
@JsonSubTypes({
@JsonSubTypes.Type(value=Dog.class, name="dog"),
@JsonSubTypes.Type(value=Cat.class, name="cat"),
})
@ResourceName("animal");
public interface AnimalDto {
/**
* Stop gap property to deal with jackson not serializing type information
* when using {@link JsonTypeInfo}
*
* @return A string name of the type of Animal
*/
@JsonProperty("_type")
}
public abstract class WildAnimal implements AnimalDto {
private final String type;
@Override
public String getType(){
return this.type;
}
}
@JsonTypeName("dog")
public class Dog implements WildAnimal {
}
@JsonTypeName("cat")
public class Cat implements WildAnimal {
}
JSON :
{
"animal": {
"_type":"dog",
"id": "1",
}
}
当我尝试反序列化上述 json 对 java 的响应时,jackson 抛出以下错误。谁能帮我解决这个问题。
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property '_type' that is to contain type id (for class Animal)
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@4b10fe3f; line: 1, column: 111]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property '_type' that is to contain type id (for class Animal)
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@4b10fe3f; line: 1, column: 111]
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:228)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.java:220)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:795)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:779)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:559)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:512)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:454)
你可以看到这个post我想你会找到你想要的:
Jackson JSON Polymorphism
PS : 小心点,你有时有“_type”,有时 "type"。
Java :
/**
*/
@JsonTypeInfo(include=As.PROPERTY, property="_type", use=Id.NAME)
@JsonSubTypes({
@JsonSubTypes.Type(value=Dog.class, name="dog"),
@JsonSubTypes.Type(value=Cat.class, name="cat"),
})
@ResourceName("animal");
public interface AnimalDto {
/**
* Stop gap property to deal with jackson not serializing type information
* when using {@link JsonTypeInfo}
*
* @return A string name of the type of Animal
*/
@JsonProperty("_type")
}
public abstract class WildAnimal implements AnimalDto {
private final String type;
@Override
public String getType(){
return this.type;
}
}
@JsonTypeName("dog")
public class Dog implements WildAnimal {
}
@JsonTypeName("cat")
public class Cat implements WildAnimal {
}
JSON :
{
"animal": {
"_type":"dog",
"id": "1",
}
}
当我尝试反序列化上述 json 对 java 的响应时,jackson 抛出以下错误。谁能帮我解决这个问题。
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property '_type' that is to contain type id (for class Animal)
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@4b10fe3f; line: 1, column: 111]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property '_type' that is to contain type id (for class Animal)
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@4b10fe3f; line: 1, column: 111]
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:228)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.java:220)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:795)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:779)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:559)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:512)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:454)
你可以看到这个post我想你会找到你想要的: Jackson JSON Polymorphism
PS : 小心点,你有时有“_type”,有时 "type"。