@JsonNaming不与lombok生成器一起工作



我有低于等级的


@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
public class Test {
String tTObj;
String value;
public String gettTObj() {
return tTObj;
}
public void settTObj(final String tTObj) {
this.tTObj = tTObj;
}
public String getValue() {
return value;
}
public void setValue(final String value) {
this.value = value;
}
public static void main(final String[] args) throws IOException {
final String json = "{"TTObj" : "hi", "Value": "hello"}";
final ObjectMapper objectMapper = new ObjectMapper();
final Test test = objectMapper.readValue(json, Test.class);
System.out.println(test);
}
}

上面的一个工作正常并且能够反序列化。

但如果我使用lombok-builder进行反序列化,它就会失败。

下面是用于反序列化的带有lombok构建器的类

@JsonDeserialize(builder = Test.Builder.class)
@Value
@Builder(setterPrefix = "with", builderClassName = "Builder", toBuilder = true)
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
public class Test {
String tTObj;
String value;
public static void main(final String[] args) throws IOException {
final String json = "{"TTObj" : "hi", "Value": "hello"}";
final ObjectMapper objectMapper = new ObjectMapper();
final Test test = objectMapper.readValue(json, Test.class);
System.out.println(test);
}
}

低于错误

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "TTObj" (class com.demo.Test$Builder), not marked as ignorable (2 known properties: "value", "ttobj"])
at [Source: (String)"{"TTObj" : "hi", "Value": "hello"}"; line: 1, column: 13] (through reference chain: com.demo.Test$Builder["TTObj"])

如何使用@JsonNaming和lombok生成器反序列化"{"TTObj" : "hi", "Value": "hello"}"this?

类上的Jackson注释也必须存在于构建器类上,才能对反序列化产生影响。Lombok 1.18.14引入了@JsonNaming到构建器类的自动复制。

但是,版本5.1.0中的io.freefair.lombokGradle插件默认使用Lombok版本1.18.12。所以这是一个太旧的版本(Lombok只使用偶数作为次要/补丁级别的数字(。

您可以使用Gradle插件的较新版本,也可以通过build.gradle:手动配置它以使用较新的Lombok版本

lombok {
version = "1.18.20"
}

附言:与龙目>=1.18.14,您可以考虑使用@Jacksonized,而不是手动配置您的构建器:

@Value
@Jacksonized
@Builder(builderClassName = "Builder", toBuilder = true)
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
public class Test {
...
}

您不需要CCD_ 6注释和";用";在这种情况下使用前缀。

相关内容

  • 没有找到相关文章

最新更新