1

I'm using Spring Boot with Jackson. I have an unusual JSON set coming in from Elasticsearch (spring-data-elasticsearch) which I need to read into a POJO.

 {  
   "thread_name":"main",
   "sort":[  
       1522270372773,
       "log#AWJuYn7SAKReCIGzMYda"
   ]
 }

Notably, the "sort" array contains a number and then a String. I'm not having any luck getting a Java pojo made correctly for this. Would the correct approach to be to declare "sort" as an array of Object? And then what does the constructor look like?


Update 6/12/18:

Adding some extra code as requested:

 import java.util.List;

 import org.springframework.data.annotation.Id;
 import org.springframework.data.elasticsearch.annotations.Document;
 import org.springframework.data.elasticsearch.annotations.Field;
 import org.springframework.data.elasticsearch.annotations.FieldType;

 import com.fasterxml.jackson.annotation.JsonProperty;

 @Document(indexName = "logstash-*", type="log")
 public class LogEntry {

@Id
private String id;

@JsonProperty("level")
private String level;

@JsonProperty("level_value")
private Integer levelValue;

@JsonProperty("host")
private String host;

@JsonProperty("cluster")
private String cluster;

@Field(type = FieldType.Nested)
@JsonProperty("@timestamp")
private String timestamp;

private String message;

@JsonProperty("application_name")
private String applicationName;

@JsonProperty("application_version")
private String applicationVersion;

@JsonProperty("thread_name")
private String threadName;

@JsonProperty("stack_trace")
private String stackTrace;

@JsonProperty("logger_name")
private String loggerName;

@JsonProperty("sort")
private List<Object> sort;

<truncated generic getters/setters>
 }

Here's the Driver which reproduces the null:

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.perspectium.elasticsearch.entities.LogEntry;

public class Test {

private static String RESPONSE = "{\n" + 
        "          \"@timestamp\": \"2018-03-28T20:52:52.773Z\",\n" + 
        "          \"application_name\": \"bleah-server\",\n" + 
        "          \"level_value\": 400,\n" + 
        "          \"thread_name\": \"main\",\n" + 
        "          \"host\": \"df58952e2cb9\",\n" + 
        "          \"logger_name\": \"com.bleah.Manager\",\n" + 
        "          \"application_version\": \"7.77.0-SNAPSHOT\"\n" + 
        "        },\n" + 
        "        \"sort\": [\n" + 
        "          1522270372773,\n" + 
        "          \"log#AWJuYn7SAKReCIGzMYda\"\n" + 
        "        ]\n" + 
        "      }";

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {

    System.out.println("Start");
    ObjectMapper objectMapper = new ObjectMapper();
    LogEntry logEntry = objectMapper.readValue(RESPONSE.getBytes(), LogEntry.class);
    System.out.println(objectMapper.writeValueAsString(logEntry));
    System.out.println("Finish.");
}

}

JvmSd121
  • 351
  • 3
  • 17
  • What about a thread Pojo that has the attributes: String thread_name, Sort sort; and Sort class has a Long id, String whatever? Why does that not map your output? – flashjpr Jun 11 '18 at 23:11
  • That was my first impulse, but "Sort" gets populated as null. I might be overthinking it, but isn't that technically modeling an object "Sort" with an id object and a string object. Technically I need a single array which holds both objects together in the same array. I assume that is my problem here but I don't know. – JvmSd121 Jun 11 '18 at 23:24
  • sort as List?? – gagan singh Jun 11 '18 at 23:33
  • You can create a custom type or you can use map if you aren't aware of K,V pair or its dynamic. – SASIKUMAR SENTHILNATHAN Jun 11 '18 at 23:41
  • Generally it's preferable to use `List`, and a `List` should work fine. – chrylis -cautiouslyoptimistic- Jun 11 '18 at 23:53
  • @JsonProperty("sort") private List sort; -- it definitely populates to null. this is one of the variations I tried – JvmSd121 Jun 12 '18 at 00:28
  • show us more code, as the comments above are valid – flashjpr Jun 12 '18 at 01:37
  • I've got to the bottom of the mess. Flashjpr is right, I introduced an error in my code while simplifying things for the forum. The JSON above is not legal. The bigger picture issue was caused by the Spring Data Elasticsearch libraries (specifically DefaultResultMapper) specifically removing the JSON section I need before serialization even tho it was provided by the server. – JvmSd121 Jun 12 '18 at 23:31

0 Answers0