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

标签 java arrays json string spring

我无法在网络服务中正确读取输入 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/

相关文章:

java - 包含标记的 ANTLR 文本 - SWIFT 换行冒号

java - 在 TimeSeries JFreeChart 上显示总时间(以秒为单位)

java - 在 java list 中设置没有快捷方式目录文件夹的类路径时出现问题

javascript - 关于 JavaScript 数组原型(prototype)

jquery - 使用 jQuery 将 JSON 转换为 XML

java - 用于查找字符串中 1 到 3 个字符的正则表达式

arrays - Perl-Postgres 列数组和引号

java - 如何使用 XPath/XSLT fn :json-to-xml

c# - 如何向json添加私有(private)字段

c - 如何释放 c 中动态分配的结构数组?