Paul Kovacs
Paul Kovacs

Reputation: 31

How to ingest Json String arrays with spring boot

I am having trouble getting my an input JSON file to be read properly within my webservice. I am attempting to change some input parameters from a simple String to an array of Strings

my input JSON looks something like this:

{
"inputParams" : { 
    "speckleFilter" : "filter1",
    "acdChangeType" : "My_ACD",
    "filterSize" : "7",
    "aoi" : "[49.219181, 49.169297, -123.21575, -123.131194]",
    "acdThreshold" : "",
    "backScatScale" : "sigma"
},
"inputData" : [
    { "stackId" : "1",
      "state" : "new",
      "uri" : "file:/C:/My/File/Name/",
      "filterParams" : {
          "aoi" : "[49.219181, 49.169297, -123.21575, -123.131194]",
          "poi" : "",
          "passDir" : "",
          "sensorType" : ["sensor1"],
          "contentType" : "[type1]",
          "mediaType" : "[mediaType1]"}},
    {
      "stackId" : "1",
      "state" : "new",
      "uri" : "file:/C:/My/File/Name/",
      "filterParams" : {
          "aoi" : "[49.219181, 49.169297, -123.21575, -123.131194]",
          "poi" : "",
          "passDir" : "",
          "sensorType" : ["sensor1"],
          "contentType" : "[type1]",
          "mediaType" : "[mediaType1]"}},
    ]
}

Specificially, sensorType will not be read in this way, and I obtain an error

Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token\n

Here are my request wrapper classes:

public class FilterParams {

private String passDir;
private List<String> sensorType;
private String aoi;
private String mediaType;
private String poi;
private String contentType;

public FilterParams(){
}

public FilterParams(String passDir,   List<String> sensorType, String aoi, 
                    String mediaType, String poi,        String contentType){
    super();
    this.passDir = passDir;
    this.sensorType = sensorType;
    this.aoi = aoi;
    this.mediaType = mediaType;
    this.poi = poi;
    this.contentType = contentType;
}

    //Getters and Setters....

}

InputData:

public class InputData {

private String       stackId; 
private String       state;
private URI          uri;
private FilterParams filterParams;

public InputData(){
}

public InputData(String stackId, String state, URI uri, FilterParams filterParams) {
    super();
    this.stackId      = stackId;
    this.state        = state;
    this.uri          = uri;
    this.filterParams = filterParams;
}
    \\Getters and Setters
}

InputParams:

public class InputParams {

private String speckleFilter;
private String acdChangeType;
private String filterSize;
private String aoi;
private String acdThreshold;
private String backScatScale;

public InputParams(){
}

public InputParams(String speckleFilter, String acdChangeType, String filterSize, 
                   String aoi,           String acdThreshold,  String backScatScale) {
    super();
    this.speckleFilter = speckleFilter;
    this.acdChangeType = acdChangeType;
    this.filterSize    = filterSize;
    this.aoi           = aoi;
    this.acdThreshold  = acdThreshold;
    this.backScatScale = backScatScale;
}
    \\Getters and Setters
}

My controller class is using @RequestBody to parse the incomming JSON request:

@RequestMapping(value="/preprocessing", method = RequestMethod.POST)
public ResponseEntity<PreProcessingResponse> doProcessing ( @RequestBody PreProcessingRequest preProcessingReq ){
    \\do important things
}

If I format my JSON file sensorType to look the same as contentType and mediaType then the JSON request is easily parsed. However, I would like to changed these attributed to be arrays of string (since more then one option should be valid)

To resolve my issue I have tried to declar the sensorType within the FilterParams class as List, ArrayList, JSONArray, String[], List, and probably a few more which I have now forgotten.

It is clear that I am completely misunderstanding how these objects are being interpreted with spring's @RequestBody annotation. Could someone help clear this up a bit for me? Why can I not just change the sensorType attribute to be an array of strings ?

This whole domain is rather new to me (I have never programmed in java before). So please let me know if something is not clear, and I will do my best to clarify.

Any help is much appreciated!

Upvotes: 0

Views: 2177

Answers (1)

Danda
Danda

Reputation: 390

@RequestBody uses Jackson library to convert JSON to POJO. I hope this might help you.

Jersey: Can not deserialize instance of ArrayList out of String

Upvotes: 1

Related Questions