[英]Spring Json deserialization to Java doesn't work
I currently facing an issue with the deserialization of json into an polymorphic type. 我目前面临将json反序列化为多态类型的问题。
This is my controller which receives a RecommendedVirtualEditionParam. 这是我的控制器,它收到了推荐的VirtualEditionParam。
@RequestMapping(
value = "/sortedEdition",
method = RequestMethod.POST,
headers = { "Content-type=application/json;charset=UTF-8"
})
public String getSortedRecommendedVirtualEdition(
Model model,
@RequestBody RecommendVirtualEditionParam params) {
//Do Stuff
}
RecommendedVirtualEditionParam is a container: 推荐的VirtualEditionParam是一个容器:
public class RecommendVirtualEditionParam {
private final String acronym;
private final String id;
private final Collection<Property> properties;
public RecommendVirtualEditionParam(
@JsonProperty("acronym") String acronym,
@JsonProperty("id") String id,
@JsonProperty("properties") Collection<Property> properties) {
this.acronym = acronym;
this.id = id;
this.properties = properties;
}
//Getters
}
Property is a polymorphic type and I believe it's the one giving my problems. 属性是一种多态类型,我相信这是给我带来麻烦的类型。
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = SpecificTaxonomyProperty.class, name = "specific-taxonomy")
})
public abstract class Property {
public Property() {
}
//Other methods
}
The sub type: 子类型:
public class SpecificTaxonomyProperty extends Property {
private final String acronym;
private final String taxonomy;
public SpecificTaxonomyProperty(
@JsonProperty("acronym") String acronym,
@JsonProperty("taxonomy") String taxonomy) {
this.acronym = acronym;
this.taxonomy = taxonomy;
}
The json being send on requests: 根据请求发送的json:
{
acronym: "afs"
id: "167503724747"
properties: [
{
type: "specific-taxonomy",
acronym: "afs",
taxonomy: "afs"
}
]
}
When I run it like this I get a org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported 当我这样运行时,我得到一个org.springframework.web.HttpMediaTypeNotSupportedException:内容类型'application / json; charset = UTF-8'不支持
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:149) ~[spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:180) ~[spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:95) ~[spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]
I believe there is something wrong with with the way I setup my Property class that makes it unable to deserialize. 我相信设置Property类的方式存在问题,导致无法反序列化。 Any has a clue and give me an hand?
有什么线索可以帮我吗?
I managed to fix my problem. 我设法解决了我的问题。
Before posting, I had looked around and a lot of answers pointed to the class I wanted to deserialize having multiple setters for the same attribute. 发布之前,我环顾四周,很多答案都指向我要反序列化的类,该类具有相同属性的多个设置方法。 I didn't pay much attention to it, since my attributes were final, I didn't have any setters for them.
我没有特别注意它,因为我的属性是最终属性,所以我没有任何设置方法。
https://stackoverflow.com/a/19444874/2364671 https://stackoverflow.com/a/19444874/2364671
But I had a method called setUserWeight(RecommendedWeights weights) which didn't set any attributes of Property, but after renaming it, my problem was fixed. 但是我有一个名为setUserWeight(RecommendedWeights weights)的方法,该方法没有设置Property的任何属性,但是在重命名它后,我的问题得以解决。
I don't quiet understand the reason for this odd behavior and would love some light about it. 我不安静地了解这种奇怪行为的原因,并且希望对此有所了解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.