[英]Mapping a JSON Array to POJO using Lombok
I've looked at similar questions but it doesn't seem to address my issue.我看过类似的问题,但似乎没有解决我的问题。 I have a JSON payload that's returned from a Feign webservice call that I'm mapping to a POJO.
我有一个 JSON 有效负载,它是从我映射到 POJO 的 Feign Web 服务调用返回的。
JSON JSON
{
"fields":[
{
"field_one":"one value",
"field_two":"two value",
},
{
"field_one":"one value",
"field_two":"two value",
}
]
}
POJO - Wrapper Class POJO - 包装器 Class
@Data
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class FieldsResponse {
public List<FieldInfo> fields;
}
POJO - Detail Class POJO-细节 Class
@Data
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class FieldInfo{
@JsonProperty("field_one")
private String fieldOne;
@JsonProperty("field_two")
private String fieldTwo;
}
The POJO's are not being populated. POJO 没有被填充。 If I change items in the wrapper POJO to a JsonArray everything works fine (ie: I can see the JSON response correctly).
如果我将包装 POJO 中的项目更改为 JsonArray 一切正常(即:我可以正确看到 JSON 响应)。 I've tried initializing the list in the wrapper object and have also experimented with using a vector instead.
我已经尝试在包装器 object 中初始化列表,并且还尝试使用向量来代替。
Any ideas?有任何想法吗?
ETA: If I remove the @JsonPropery("field_one") mapping and rename the variable from fieldOne to field_one then it works. ETA:如果我删除@JsonPropery("field_one")映射并将变量从fieldOne重命名为field_one那么它可以工作。 But this is not how I want it to work.
但这不是我想要的工作方式。
The code below worked fine.下面的代码运行良好。
Versions:版本:
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.List;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
@Log4j2
public class Q62195156 {
// @formatter:off
static final String JSON="{\"fields\":[{\"field_one\":\"one value\",\"field_two\":\"two value\"},{\"field_one\":\"one value\",\"field_two\":\"two value\"}]}";
// @formatter:on
static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
@Test
void test() throws JsonMappingException, JsonProcessingException {
var fieldsResponse = OBJECT_MAPPER.readValue(JSON, FieldsResponse.class);
LOGGER.info("fieldResponse: {}", fieldsResponse);
var fields = fieldsResponse.getFields();
LOGGER.info("fields: {}", fieldsResponse);
assertNotNull(fields, "fields");
var fieldInfo0 = fields.get(0);
LOGGER.info("fieldInfo0: {}", fieldInfo0);
assertNotNull(fieldInfo0, "fieldInfo0");
assertEquals(fieldInfo0.getFieldOne(), "one value", "fieldInfo0.getFieldOne()");
assertEquals(fieldInfo0.getFieldTwo(), "two value", "fieldInfo0.getFieldTwo()");
var fieldInfo1 = fields.get(1);
LOGGER.info("fieldInfo1: {}", fieldInfo1);
assertNotNull(fieldInfo1, "fieldInfo1");
assertEquals(fieldInfo1.getFieldOne(), "one value", "fieldInfo1.getFieldOne()");
assertEquals(fieldInfo1.getFieldTwo(), "two value", "fieldInfo1.getFieldTwo()");
}
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
static class FieldsResponse {
public List<FieldInfo> fields;
}
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
static class FieldInfo {
@JsonProperty("field_one")
private String fieldOne;
@JsonProperty("field_two")
private String fieldTwo;
}
}
Results:结果:
13:14:42.344 [main] INFO io.jeffmaxwell.stackoverflow.Q62195156 - fieldResponse: Q62195156.FieldsResponse(fields=[Q62195156.FieldInfo(fieldOne=one value, fieldTwo=two value), Q62195156.FieldInfo(fieldOne=one value, fieldTwo=two value)])
13:14:42.347 [main] INFO io.jeffmaxwell.stackoverflow.Q62195156 - fields: Q62195156.FieldsResponse(fields=[Q62195156.FieldInfo(fieldOne=one value, fieldTwo=two value), Q62195156.FieldInfo(fieldOne=one value, fieldTwo=two value)])
13:14:42.349 [main] INFO io.jeffmaxwell.stackoverflow.Q62195156 - fieldInfo0: Q62195156.FieldInfo(fieldOne=one value, fieldTwo=two value)
13:14:42.351 [main] INFO io.jeffmaxwell.stackoverflow.Q62195156 - fieldInfo1: Q62195156.FieldInfo(fieldOne=one value, fieldTwo=two value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.