[英]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. 我无法在网络服务中正确读取输入的JSON文件。 I am attempting to change some input parameters from a simple String to an array of Strings
我正在尝试将一些输入参数从简单的String更改为Strings的数组
my input JSON looks something like this: 我的输入JSON看起来像这样:
{
"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 具体来说,将不会以这种方式读取sensorType,并且我得到一个错误
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: 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: 我的控制器类正在使用@RequestBody解析传入的JSON请求:
@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. 如果我将JSON文件sensorType的格式设置为与contentType和mediaType相同,则可以轻松解析JSON请求。 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. 为了解决我的问题,我尝试将FilterParams类中的sensorType声明为List,ArrayList,JSONArray,String [],List,以及可能现在我忘记的其他几个。
It is clear that I am completely misunderstanding how these objects are being interpreted with spring's @RequestBody annotation. 显然,我完全误解了如何使用spring的@RequestBody注释解释这些对象。 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 ?
为什么不能只将sensorType属性更改为字符串数组?
This whole domain is rather new to me (I have never programmed in java before). 这整个领域对我来说还是很新的(我以前从未用Java编程过)。 So please let me know if something is not clear, and I will do my best to clarify.
因此,如果有不清楚的地方,请告诉我,我会尽力澄清。
Any help is much appreciated! 任何帮助深表感谢!
@RequestBody uses Jackson library to convert JSON to POJO. @RequestBody使用Jackson库将JSON转换为POJO。 I hope this might help you.
希望对您有所帮助。
Jersey: Can not deserialize instance of ArrayList out of String 泽西岛:无法从字符串中反序列化ArrayList的实例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.