我正在根据我的要求使用ObjectMapper创建json。我选择这个,因为我有现有的DTO。有人可以让我知道使用POJO而不是通过对象节点创建对象吗?
下面是我想用POJO重写的现有代码片段。
ObjectMapper mapper = new ObjectMapper()
mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
ObjectNode orderNode = new ObjectNode(mapper.getNodeFactory());
ObjectNode order = new ObjectNode(mapper.getNodeFactory());
ArrayNode requests = new ArrayNode(mapper.getNodeFactory());
for (OrderDto dto : searchResults.data) {
order.put("order-id", dto.id);
order.put("order-ext-id", dto.externalId);
order.put("from-address", dto.from == null ? null : dto.from.placeId);
order.put("to-address", dto.to == null ? null : dto.to.placeId);
order.put("available-time", dto.availableBy);
order.put("committed-time", dto.committedBy);
for (RequestDto requestDto : dto.requests) {
ObjectNode request = new ObjectNode(mapper.getNodeFactory());
request.put("request-id", requestDto.id);
request.put("request-ext-id", requestDto.externalId);
requests.add(request);
}
order.set("requests", requests);
orderNode.set("order", order);
}
return ok(orderNode);