我有以下两个类:
public class User {
private String name;
private Secret secret;
public User( @JsonProperty("name") String name, @JsonProperty("secret") Secret secret ) {
this.name = name;
this.secret = secret;
}
public String getName() {
return name;
}
public Secret getSecret() {
return secret;
}
}
和
public class Secret {
private byte[] secret;
public Secret( byte[] secret ) {
this.secret = secret;
}
@JsonValue
public byte[] getSecret() {
return secret;
}
}
我想使用这些类来序列化/反序列化以下json:
{
"name": "bdf",
"secret": "AQ=="
}
Java to json工作正常。但是当我试图反序列化json时,我得到了以下异常:
com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class deserialization.Secret] from String value ('YQ=='); no single-String constructor/factory method
at [Source: [B@3b938003; line: 1, column: 25] (through reference chain: deserialization.User["secret"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:875)
at com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:281)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:284)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1176)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:143)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:134)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeWithErrorWrapping(BeanDeserializer.java:461)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:376)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1099)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:294)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:131)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3702)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2807)
at deserialization.SerializationTest.itShouldDeserialize(SerializationTest.java:22)
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:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
我如何告诉Jackson先解码base64编码的值,然后再使用这个构造函数?
public Secret( byte[] secret )
2条答案
按热度按时间pftdvrlh1#
这可以通过编写您自己的自定义序列化程序和反序列化程序来完成。
http://www.baeldung.com/jackson-custom-serialization
http://www.baeldung.com/jackson-deserialization
下面是一个与您所做的类似的示例...
保持器类别
字节类
请注意自定义序列化程序的注解...
序列化程序
这会将“Bytes”对象序列化为base64字符串...
解串器
主方法
一个简单的测试方法...
输出
自己运行
上面的所有类都是“静态”的,因为我将它们 Package 到一个名为“Stack”的大类中。
如果你想运行这个,创建一个新的类(你想叫什么就叫什么),然后把这里的所有代码粘贴到它里面...
2ledvvac2#
我不能使用 Package 对象,但我能够发现它是没有必要的;以下内容对我有效: