使用 Lombok 将 JSON 数组映射到 POJO

Mapping a JSON Array to POJO using Lombok

我看过类似的问题,但似乎没有解决我的问题。 我有一个 JSON 负载,它是从我映射到 POJO 的 Feign 网络服务调用返回的。

JSON

{
   "fields":[
      {
        "field_one":"one value",
         "field_two":"two value",
      },
      {
         "field_one":"one value",
         "field_two":"two value",
      }
   ]
}

POJO - 包装器 Class

@Data
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class FieldsResponse {

    public List<FieldInfo> fields;

}

POJO - 详细信息 Class

@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 响应)。我已经尝试在包装器对象中初始化列表,并且还尝试使用向量代替。

有什么想法吗?

ETA:如果我删除 @JsonPropery("field_one") 映射并将变量从 fieldOne 重命名为 field_one 然后就可以了。但这不是我希望它工作的方式。

下面的代码运行良好。

版本:

  • 采用OpenJDK 14
  • 日食:2020-03 (4.15.0)
  • junit: 5.6.2
  • log4j2: 2.13.3
  • 杰克逊: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;
    }
}

结果:

13:14:42.344 [main] INFO  io.jeffmaxwell.Whosebug.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.Whosebug.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.Whosebug.Q62195156 - fieldInfo0: Q62195156.FieldInfo(fieldOne=one value, fieldTwo=two value)
13:14:42.351 [main] INFO  io.jeffmaxwell.Whosebug.Q62195156 - fieldInfo1: Q62195156.FieldInfo(fieldOne=one value, fieldTwo=two value)