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 - Wrapper Class
@Data
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class FieldsResponse {
public List<FieldInfo> fields;
}
POJO - Detail 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. 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.
Any ideas?
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With