我正在尝试结合杰克逊和龙目岛。这些是我的课程:
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(onConstructor=@__(@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-annotations/2.8.2/jackson-annotations-2.8.2.jar
杰克逊核心: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/com/fasterxml/jackson/core/jackson-databind/2.8.2/jackson-databind-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注释处理”和“在编辑器中进行Activacte注释处理”。Netbeans中未显式配置任何注释处理器或注释处理选项。另外,-Xlint:all
在编译器命令行中传递了“ ”命令行选项,并且编译器在外部VM上运行。
我的javac版本是1.8.0_72,而我的java版本是1.8.0_72-b15。我的Netbeans是8.1。
我的项目编译正常。但是,它在执行过程中引发异常。似乎没有什么异常或显而易见的可修复异常。这是输出,包括stacktrace:
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 java.beans.ConstructorProperties=@java.beans.ConstructorProperties(value=[x, z]), interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jackson.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 java.beans.ConstructorProperties=@java.beans.ConstructorProperties(value=[x, z]), interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jackson.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
注释随意戳戳,但是我无法做得更好。
我用谷歌搜索了一个异常,发现了一个关于杰克逊的旧错误报告,另一个是打开的,但似乎与其他东西有关。但是,这仍然不能说明该错误是什么或如何修复。另外,在其他地方找不到任何有用的东西。
由于我想做的是龙目岛和杰克逊的基本用法,所以我找不到关于如何解决此问题的更多有用信息,这似乎很奇怪。也许我错过了什么?
除了只说“不要使用龙目岛”或“不要使用杰克逊”外,有人对如何解决这个问题有任何想法吗?