如何解决Lombok:为 JSON 创建 POJO,包括未定义的值映射
我正在尝试使用 Lombok 将 JSON 转换为 POJO
JSON 架构:
"definedKey": {
"undefinedKey1": "value1","undefinedKey2": "value2"
// ...
}
definedKey
中的 K-V 可以为空或有 N 个 undefinedKeys
。
我通过以下方式为 definedKey
和 undefinedKey
创建了 POJO:
@Getter
@Setter
public class ParentDTO {
private DefinedKey definedKey;
}
@Getter
@Setter
public class DefinedKey {
private Map<String,String> inputValue;
}
我以这种方式插入:
Map<String,String> undefinedKeyMap = Map.ofEntries(
entry("Test","Test Value")
);
ParentDTO.setDefinedKey(undefinedKeyMap);
//...
RequestEntity<T> requestEntity = RequestEntity.post(uri)
.body(parentDTOBuilder); // Building ParentDTO with appropriate values
但是当我运行一个 API 时,它似乎没有选择这个值。
我对原始 JSON 使用了相同的方法,并且可以正常工作:
public void postRawJSON() {
HttpEntity<String> entity = new HttpEntity<>(getRequestJson());
String response = restTemplate.postForObject(url,entity,String.class);
}
private String getRequestJson() {
return "{\"definedKey\":{\"undefinedKey1\":\"value1\"}}";
}
有没有办法使用 Lombok 构建 JSON,还是应该求助于 Object Mapper?如果是这样,我该如何实现?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。