我在結合傑克遜和龍目島進行實驗。這些都是我的課:不能讓傑克遜和龍目一起工作
package testelombok;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Value;
import lombok.experimental.Wither;
@Value
@Wither
@AllArgsConstructor([email protected]__(@JsonCreator))
public class TestFoo {
@JsonProperty("xoom")
private String x;
private int z;
}
package testelombok;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xebia.jacksonlombok.JacksonLombokAnnotationIntrospector;
import java.io.IOException;
public class TestLombok {
public static void main(String[] args) throws IOException {
TestFoo tf = new TestFoo("a", 5);
System.out.println(tf.withX("b"));
ObjectMapper om = new ObjectMapper().setAnnotationIntrospector(new JacksonLombokAnnotationIntrospector());
System.out.println(om.writeValueAsString(tf));
TestFoo tf2 = om.readValue(om.writeValueAsString(tf), TestFoo.class);
System.out.println(tf2);
}
}
這些都是我加入到CLASSPTH JAR文件:
龍目島:https://projectlombok.org/downloads/lombok.jar(版本1.16.10)
傑克遜核心:http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.8.2/jackson-core-2.8.2.jar
傑克遜 - 龍目島:http://repo1.maven.org/maven2/io/paradoxical/jackson-lombok/1.1/jackson-lombok-1.1.jar
我使用Netbeans編譯它(我不認爲這真的是相關的,但我這個報告無論如何,使其完美和忠實可再現)。上述五個JAR保存在項目文件夾內的「lib
」文件夾中(以及「src
」,「nbproject
」,「test
」和「build
」)。我通過項目屬性中的「添加JAR /文件夾」按鈕將它們添加到Netbeans中,並按照上面列表中的順序列出它們。該項目是一個標準的「Java應用程序」類型的項目。
此外,NetBeans項目配置爲 「不要在保存時編譯」, 「生成調試信息」, 「報告已棄用的API」, 「跟蹤Java的依賴」,「activacte註解在編輯器中激活「和」激活註解處理「。 Netbeans中沒有明確配置註釋處理器或註釋處理選項。另外,在編譯器命令行中傳遞「-Xlint:all
」命令行選項,編譯器在外部VM上運行。
我的javac版本是1.8.0_72,我的java版本是1.8.0_72-b15。我的Netbeans是8.1。
我的項目編譯得很好。但是,它在執行中會引發異常。這個例外似乎不是任何看起來很容易或明顯可修復的東西。下面是輸出,包括堆棧跟蹤:
TestFoo(x=b, z=5)
{"z":5,"xoom":"a"}
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Argument #0 of constructor [constructor for testelombok.TestFoo, annotations: {interface [email protected]es(value=[x, z]), interface [email protected]son.annotation.JsonCreator(mode=DEFAULT)}] has no property name annotation; must have name when multiple-parameter constructor annotated as Creator
at [Source: {"z":5,"xoom":"a"}; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:296)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:269)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:475)
at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:3890)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3785)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2833)
at testelombok.TestLombok.main(TestLombok.java:14)
Caused by: java.lang.IllegalArgumentException: Argument #0 of constructor [constructor for testelombok.TestFoo, annotations: {interface [email protected]es(value=[x, z]), interface [email protected]son.annotation.JsonCreator(mode=DEFAULT)}] has no property name annotation; must have name when multiple-parameter constructor annotated as Creator
at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory._addDeserializerConstructors(BasicDeserializerFactory.java:511)
at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory._constructDefaultValueInstantiator(BasicDeserializerFactory.java:323)
at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory.findValueInstantiator(BasicDeserializerFactory.java:253)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:219)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:141)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:406)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:352)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
... 7 more
我已經試了一下隨機與@Value
和@AllArgsConstructor
註釋戳,但我不能讓任何好轉。
我googled例外和found an old bug report on jackson和another one that is open, but seems to be related to something else。但是,這仍然沒有說明什麼是這個錯誤或如何解決它。另外,我找不到其他地方有用的東西。
因爲我想要做的是龍目和傑克遜的非常基本的用法,似乎很奇怪,我找不到有關如何解決此問題的更多有用信息。也許我錯過了什麼?
除了只是說「不使用龍目島」或「不使用傑克遜」,做任何人有任何關於如何解決這個任何想法?
'suppressConstructorProperties'現在已經過時:-( – lilalinux