我試圖修補一個對象,它與另一個多態對象有OneToOne關係。擁有關係的對象看起來是這樣的(我省略了不相關的JPA批註和域):JsonMappingException當修補到Spring Data REST庫時
public class Policy extends BaseEntity {
@Valid
@NotNull
@OneToOne(optional = false, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, orphanRemoval = true)
@JoinColumn(name = "CHANNELFLOW_ID")
private ChannelFlowConfig channelFlow;
}
引用的對象是這樣的:
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "type")
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "CHANNELFLOWCONFIG")
public abstract class ChannelFlowConfig extends BaseEntity {
@JsonProperty("type")
public String getType() {
return getClass().getName();
}
}
子對象有沒有什麼特別的,只是一些不同的字段(同樣,字段在上面省略),它們沒有Repository,而是全部通過PolicyRepository處理。通過CocoaRest和修補時
Caused by: com.fasterxml.jackson.databind.JsonMappingException:
Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'type' that is to contain type id (for class com.vw.mbbc.authserver.model.policy.ChannelFlowConfig)
at [Source: N/A; line: -1, column: -1] (through reference chain: com.vw.mbbc.authserver.model.policy.Policy["channelFlow"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270) ~[jackson-databind-2.8.9.jar:2.8.9]
at com.fasterxml.jackson.databind.DeserializationContext.wrongTokenException(DeserializationContext.java:1376) ~[jackson-databind-2.8.9.jar:2.8.9]
at com.fasterxml.jackson.databind.DeserializationContext.reportWrongTokenException(DeserializationContext.java:1197) ~[jackson-databind-2.8.9.jar:2.8.9]
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedUsingDefaultImpl(AsPropertyTypeDeserializer.java:165) ~[jackson-databind-2.8.9.jar:2.8.9]
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:105) ~[jackson-databind-2.8.9.jar:2.8.9]
at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserializeWithType(AbstractDeserializer.java:209) ~[jackson-databind-2.8.9.jar:2.8.9]
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:502) ~[jackson-databind-2.8.9.jar:2.8.9]
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:104) ~[jackson-databind-2.8.9.jar:2.8.9]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:240) ~[jackson-databind-2.8.9.jar:2.8.9]
at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1628) ~[jackson-databind-2.8.9.jar:2.8.9]
at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1301) ~[jackson-databind-2.8.9.jar:2.8.9]
at org.springframework.data.rest.webmvc.json.DomainObjectReader.doMerge(DomainObjectReader.java:222) ~[spring-data-rest-webmvc-2.5.5.RELEASE.jar:?]
at org.springframework.data.rest.webmvc.json.DomainObjectReader.read(DomainObjectReader.java:77) ~[spring-data-rest-webmvc-2.5.5.RELEASE.jar:?]
出現此錯誤: 現在,當我試圖修補策略與具有從/政策/ 2 GET獲取完全相同的內容身體,我得到這個例外通過改造進行修補時。有趣的是客戶端(Retrofit)能夠正確地反序列化JSON(傑克遜版本在客戶端和服務器端是相同的)。 內容發送看起來像這樣:
{
"id": 2,
/* some fields */
"channelFlow": {
"id": 1,
/* some fields */
"type": "com.somepackage.VehicleChannelFlowConfig"
}
}
任何想法,我做錯了嗎? 謝謝!
編輯:即使當我在服務器手動註冊ObjectMapper,它能夠反序列化JSON。我出出主意吧:-(
編輯#2: 問題是Spring的DomainObjectReader,代碼
if (!mappedProperties.hasPersistentPropertyForField(fieldName)) {
i.remove();
}
刪除正確傳輸類型的註釋這似乎與Spring本身有問題,傑克遜工作正常
READ_ONLY可悲的是不會改變任何東西:-( – N4zroth