问题描述
我看过类似的问题,但似乎无法解决我的问题.我有一个Feign网络服务调用返回的JSON负载,该负载已映射到POJO.
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
{
"fields":[
{
"field_one":"one value",
"field_two":"two value",
},
{
"field_one":"one value",
"field_two":"two value",
}
]
}
POJO-包装器类
@Data
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class FieldsResponse {
public List<FieldInfo> fields;
}
POJO-详细类
@Data
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class FieldInfo{
@JsonProperty("field_one")
private String fieldOne;
@JsonProperty("field_two")
private String fieldTwo;
}
未填充POJO.如果我将包装器POJO中的项目更改为JsonArray,则一切正常(即:我可以正确看到JSON响应).我尝试初始化包装对象中的列表,还尝试使用向量代替.
The POJO's are not being populated. If I change items in the wrapper POJO to a JsonArray everything works fine (i.e.: I can see the JSON response correctly). I've tried initializing the list in the wrapper object and have also experimented with using a vector instead.
有什么想法吗?
ETA:如果我删除 @JsonPropery("field_one")映射并将变量从 fieldOne 重命名为 field_one ,那么它将起作用.但这不是我想要的方式.
ETA: If I remove the @JsonPropery("field_one") mapping and rename the variable from fieldOne to field_one then it works. But this is not how I want it to work.
推荐答案
下面的代码工作正常.
版本:
Versions:
- 采用OpenJDK 14
- Eclipse:2020-03(4.15.0)
- junit:5.6.2
- log4j2:2.13.3
- jackson:2.11.0
- 龙目岛:1.18.12
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)
这篇关于使用Lombok将JSON数组映射到POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!