我正在使用不可变库(https://immutables.github.io)。
我的班级如下所示:
package com.abc.myservice.data.models;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.immutables.value.Value;
import java.util.Map;
import java.util.Optional;
@Value.Immutable
@JsonSerialize(as = ImmutableMyEntityModel.class)
@JsonDeserialize(as = ImmutableMyEntityModel.class)
public interface MyEntityModel {
String myEntityId();
String status();
Optional<Integer> count();
Optional<Integer> version();
Optional<Map<String, String>> attributes();
}
我使用以下方法构建不可变类对象:
ImmutableMyEntityModel.builder()
.myEntityId("some-id")
.status("some-status")
.count(Optional.of(10))
.build()
我的输出是:
{
"MyEntityId": "some-id",
"status": "some-status",
"count": {
"present": true
},
"version": {
"present": false
},
"attributes": {
"present": false
}
}
相反,我想看到的是:
{
"MyEntityId": "some-id",
"status": "some-status",
"count": 10
}
我怎样才能让它像那样工作?