我无法在我的webservice中正确读取输入的JSON文件。我试图将一些输入参数从简单的String更改为Strings数组
我的输入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]"}},
]
}
具体来说,不会以这种方式读取sensorType,我会收到错误
Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token\n
以下是我的请求包装类:
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
}
我的控制器类使用@RequestBody来解析崩溃的JSON请求:
@RequestMapping(value="/preprocessing", method = RequestMethod.POST)
public ResponseEntity<PreProcessingResponse> doProcessing ( @RequestBody PreProcessingRequest preProcessingReq ){
\\do important things
}
如果我将我的JSON文件sensorType格式化为与contentType和mediaType相同,则可以轻松解析JSON请求。但是,我想将这些归结为字符串数组(因为多于一个选项应该是有效的)
为了解决我的问题,我尝试将FilterParams类中的sensorType声明为List,ArrayList,JSONArray,String [],List,还有一些我现在已经忘记的内容。
很明显,我完全误解了如何使用spring的@RequestBody注释来解释这些对象。有人可以帮我解决这个问题吗?为什么我不能只将sensorType属性更改为字符串数组?
这整个域对我来说都是新的(我之前从未在java中编程)。如果有些事情不清楚,请告诉我,我会尽力澄清。
非常感谢任何帮助!
答案 0 :(得分:1)
@RequestBody使用Jackson库将JSON转换为POJO。我希望这对你有帮助。
Jersey: Can not deserialize instance of ArrayList out of String