【问题标题】:How to ingest Json String arrays with spring boot如何使用 Spring Boot 摄取 Json 字符串数组
【发布时间】:2016-04-04 23:28:01
【问题描述】:

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

我的控制器类正在使用 @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 中编程过)。所以如果有不清楚的地方请告诉我,我会尽力澄清。

非常感谢任何帮助!

【问题讨论】:

标签: java arrays json string spring


【解决方案1】:

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

Jersey: Can not deserialize instance of ArrayList out of String

【讨论】:

  • 感谢您的回复,虽然上面的链接没有直接帮助,但我在实际的 JSON 文件中发现了一个问题 :( 我的经验不足在这一刻真的很突出。
猜你喜欢
  • 2015-06-01
  • 2022-01-13
  • 2017-06-23
  • 2022-01-09
  • 1970-01-01
  • 2020-12-30
  • 2017-05-22
  • 2018-01-12
  • 2020-09-24
相关资源
最近更新 更多