在我的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
}
你知道为什么以及该怎么做吗? 谢谢
您应该在 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