gpt4 book ai didi

java - typeinfo 的多态 Jackson 反序列化不适用于枚举

转载 作者:行者123 更新时间:2023-12-01 17:48:03 26 4
gpt4 key购买 nike

我有以下类作为 JSON 对象的 java 表示:

public class Vehicle
{
private VehicleType vehicleType;

private VehicleConfiguration vehicleConfiguration;

@JsonCreator
public Vehicle( @JsonProperty("vehicleType")final VehicleType vehicleType,
@JsonProperty("vehicleConfiguration")final VehicleConfiguration vehicleConfiguration )
{
this.vehicleType = vehicleType;
this.vehicleConfiguration = vehicleConfiguration;
}

...

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "vehicleType")
@JsonSubTypes({ @JsonSubTypes.Type(value = TruckConfiguration.class, name = "TRUCK"),
@JsonSubTypes.Type(value = CarConfiguration.class, name = "CAR") })
public void setVehicleConfiguration(
final VehicleConfiguration vehicleConfiguration )
{
this.vehicleConfiguration = vehicleConfiguration;
}
}

我使用vehicleType枚举(有两个值TRUCK和CAR)作为外部属性来告诉反序列化器我希望如何反序列化VehicleConfiguration抽象类:要么是CarConfiguration,要么是TruckConfiguration。以下是其他类(不包括样板):

public class CarConfiguration extends VehicleConfiguration
{
private int numberOfDoors;


@JsonCreator
public CarConfiguration( @JsonProperty("numberOfDoors") final int numberOfDoors )
{
this.numberOfDoors = numberOfDoors;
}
}
public class TruckConfiguration extends VehicleConfiguration
{
private String manufacturer;


@JsonCreator
public TruckConfiguration( @JsonProperty("manufacturer") final String manufacturer )
{
this.manufacturer = manufacturer;
}
}

我的测试如下:

@Test
public void given_when_then() throws IOException
{
// GIVEN
final String json = "{\n"
+ " \"vehicleType\": \"TRUCK\",\n"
+ " \"vehicleConfiguration\": {\n"
+ " \"manufacturer\": \"SCANIA\"\n"
+ " }\n"
+ "}";

// WHEN
ObjectMapper mapper = new ObjectMapper();
mapper.enableDefaultTyping();

Vehicle vehicle = mapper.readValue(json, Vehicle.class);

// THEN
assertEquals((( TruckConfiguration ) vehicle.getVehicleConfiguration()).getManufacturer(), "SCANIA");
}

它抛出以下异常:

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.test.Vehicle, problem: argument type mismatch
at [Source: {
"vehicleType": "TRUCK",
"vehicleConfiguration": {
"manufacturer": "SCANIA"
}
}; line: 6, column: 1]

at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:277)
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1441)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.wrapAsJsonMappingException(StdValueInstantiator.java:476)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.rewrapCtorProblem(StdValueInstantiator.java:495)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:276)
at com.fasterxml.jackson.databind.deser.ValueInstantiator.createFromObjectWith(ValueInstantiator.java:228)
at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:135)
at com.fasterxml.jackson.databind.deser.impl.ExternalTypeHandler.complete(ExternalTypeHandler.java:230)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeUsingPropertyBasedWithExternalTypeId(BeanDeserializer.java:937)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeWithExternalTypeId(BeanDeserializer.java:792)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:312)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:148)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2842)
at com.test.VehicleTest.given_when_then(VehicleTest.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.fasterxml.jackson.databind.introspect.AnnotatedConstructor.call(AnnotatedConstructor.java:124)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:274)
... 32 more

似乎问题在于枚举vehicleType作为typeInfo:如果我将此字段更改为简单字符串,并运行相同的测试,它就可以正常工作。但在我的设计中,我已经有一个枚举,我需要用它来区分反序列化。

我的临时解决方案是向抽象VehicleConfiguration添加一个vehicleType字段,并使用JsonTypeInfo.As.PROPERTY注释直接在抽象类上设置注释。像这样,它甚至可以与枚举一起使用,但我在vehicleType字段中添加了冗余信息。那么这是解串器的一些错误,还是我遗漏了一些东西?

最佳答案

经过大量谷歌搜索和努力,终于找到了解决方案:删除了构造函数,现在一切正常。

关于java - typeinfo 的多态 Jackson 反序列化不适用于枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60833237/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com