使用带有 JsonNullable 字段的 mapstruct 时看不到该值

问题描述 投票:0回答:1

在我的mapstruct接口中,我有一个方法将Long转换为JsonNullable,这是由swagger生成的类等待的类型(因为nullabe = true):

    default JsonNullable<BigDecimal> map(Long value) {
        return value != null ? JsonNullable.of(BigDecimal.valueOf(value)) : JsonNullable.undefined();
    }

当我处于调试状态时,没关系,JsonNullable 具有:present=true,value=80 例如。
当我使用休息客户端进行测试时,我在响应中没有值:

"montant": {
            "present": true
        }

你知道为什么以及该怎么做吗? 谢谢

java swagger mapstruct
1个回答
0
投票

基于 jackson-databind-nullable

的自述文件

您应该在 Jackson 上注册该模块

JsonNullableModule
ObjectMapper

ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.registerModule(new JsonNullableModule());

不再以编程方式执行此操作,您可以定义模块的 bean:

@Configuration
public class JacksonConfiguration {

    @Bean
    public JsonNullableModule jsonNullableModule() {
        return new JsonNullableModule();
    }

}

这将自动注册,感谢 JacksonAutoConfiguration

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.