Lombok Builder和Jackson的默认值 [英] Lombok Builder and Jackson Default values

查看:82
本文介绍了Lombok Builder和Jackson的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从序列化中排除一些默认属性.我有以下课程

I want to exculde some default properties from serialization. I have the following class

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;

@JsonInclude(Include.NON_DEFAULT)
@Builder
public class RunTime
{

@JsonProperty
@Builder.Default
private String mystring = "any";

@JsonProperty
@Builder.Default
private Boolean myboolean = true;

@JsonProperty
@JsonInclude(Include.ALWAYS)
@Builder.Default
private Boolean always = true;

}

我另外写了这个测试:

    @Test
    public void test() throws JsonProcessingException
    {
    RunTime rac = RunTime.builder().build();
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rac);

    assertThat(json).doesNotContain("mystring");
    assertThat(json).doesNotContain("myboolean");
    assertThat(json).contains("always");
   }

我只想在json字符串中包含布尔值 always (总是).不幸的是,测试失败了,因为包括了每个属性.但是,当我删除Lombok构建器并自己实例化该对象时,我注意到所有这些工作正常.我如何在Lombok上使用它?

I only want to include the boolean always property in the json string. Unfortunately, the test is failing as every property is included. However, I noticed all this is working fine when I remove the Lombok builder and instantiate the object by myself. How can I get this working with Lombok?

推荐答案

我不确定您是否找到了答案,但是我确定为什么不起作用.

I am not sure if you found an answer for this but I have determined WHY it doesn't work.

如果您对该过程感兴趣,那么我会继续使用构建器和@ Builder.Default分发源代码.

If youre interested in the process I went ahead and delomboked the source with a builder and @Builder.Default.

java -jar lombok.jar delombok -p someFile.java

似乎带有默认设置器的字段变量

It seems that a field variable with a default setter

@Builder.Default String someValue = "defaultVar"

有效地变成

String someValue;

@java.lang.SuppressWarnings("all")
private static Optional<String> $default$someValue() {
    return "defaultVar";
}

含义Lombok在做一些骇人听闻的事情,它将实例化从字段变量本身拉到Builder的build()调用中.它会检查是否在构建器中设置了暂存"值,如果未设置,则调用 $ default $ someValue()以获取该默认值.

Meaning Lombok is doing some hacky thing where it pulls the instantiation from the field variable itself into the build() call of the Builder. It does a check if the "staged" value in the builder is set and if not calls $default$someValue() to get that default value.

这意味着杰克逊序列化将无法设置您指定的默认属性.它应该调用主类的默认构造函数,并将这些值有效地设置为null.

This means the jackson serialization will fail to set your specified default properties. It should call the default constructor of your main class and effectively set those values to null.

不幸的是,我希望lombok团队意识到这一点,但是您将需要创建自己的AllArgsConstructor或NoArgsConstructor来显式设置默认值.

A little unfortunate and I am hoping the lombok team is aware of this, but you will need to create your own AllArgsConstructor or your NoArgsConstructor that sets the default values explicitly.

这篇关于Lombok Builder和Jackson的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆