gpt4 book ai didi

java - 如何使用 Spring Boot 摄取 Json 字符串数组

转载 作者:行者123 更新时间:2023-12-01 10:06:33 26 4
gpt4 key购买 nike

我无法在网络服务中正确读取输入 JSON 文件。我正在尝试将一些输入参数从简单的字符串更改为字符串数组

我的输入 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....

}

输入数据:

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
}

输入参数:

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
}

我的 Controller 类正在使用 @RequestBody 来解析传入的 JSON 请求:

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

如果我将 JSON 文件sensorType 格式化为与 contentType 和 mediaType 相同,则可以轻松解析 JSON 请求。但是,我想将这些属性更改为字符串数组(因为不止一个选项应该有效)

为了解决我的问题,我尝试将 FilterParams 类中的传感器类型声明为 List、ArrayList、JSONArray、String[]、List 以及可能还有一些我现在已经忘记的类型。

很明显,我完全误解了如何使用 spring 的 @RequestBody 注释解释这些对象。有人可以帮我解决一下这个问题吗?为什么我不能将sensorType 属性更改为字符串数组?

这整个领域对我来说相当新(我以前从未用 java 编程过)。因此,如果有不清楚的地方,请告诉我,我会尽力澄清。

非常感谢任何帮助!

最佳答案

@RequestBody 使用 Jackson 库将 JSON 转换为 POJO。我希望这对您有帮助。

Jersey: Can not deserialize instance of ArrayList out of String

关于java - 如何使用 Spring Boot 摄取 Json 字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36414626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com