【问题标题】:Parsing complicated JSON request in Spring在 Spring 中解析复杂的 JSON 请求
【发布时间】:2020-01-23 19:57:37
【问题描述】:

我正在使用 Java 和 Spring-Boot 开发 RESTful 服务,但遇到了问题。我收到了一个请求正文,例如:

    {
     "key":{
       "par1":"val1",
       "par2":"val2"
      },
     "data":{
        "par1":"val1",
        "par2":"val"
      }
   }

为了解析它,我需要创建自己的 @RequestBody 类型。就是这样:

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.util.JSONWrappedObject;

import java.io.Serializable;
import java.util.Map;

public class UpdateInfo {

    private Map<String, Object> mapKey;
    private Map<String, Object> mapData;

    ObjectMapper mapper;

    public void setMapKey(JsonNode key) {
        this.mapKey = mapper.convertValue(key,  new TypeReference<Map<String, Object>>(){});
    }

    public void setMapData(JsonNode) {
        this.mapData = mapper.convertValue(data,  new TypeReference<Map<String, Object>>(){});
    }

    public Map<String, Object> getKeys() {
        return mapKey;
    }

    public Map<String, Object> getData() {
        return mapData;
    }

}

逻辑上它必须工作,但我收到错误:

    [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: null; nested exception is com.fasterxml.jackson.databind.JsonMappingException: N/A
 at [Source: (PushbackInputStream); line: 6, column: 3] (through reference chain: com.sas.rus.spm.UpdateInfo["key"])]

实在找不到原因,希望得到您的帮助

【问题讨论】:

  • 你的键和数据对象是否总是有这两个同名的键? IE:这里提到的 par1 和 par2?

标签: java json spring rest parsing


【解决方案1】:

试试吧:

public class UpdateInfo {

    private Map<String, Object> key;
    private Map<String, Object> data;
    //getter and setter

    public Map<String, Object> getKey(){
        return key;
    }

    public void setKey(Map<String,Object> key){
         this.key = key
    }

    public Map<String, Object> getData(){
        return data;
    }

    public void setData(Map<String,Object> data){
         this.data = data
    }

}

【讨论】:

  • 那么二传手应该是什么样子?会有什么论据?
  • @Turlarion 一张地图。
  • 你能举个简短的例子吗?我还是不明白参数的类型
  • @Turlarion 使用标准的 getter 和 setter
【解决方案2】:

JSON 解析错误:null 这只是因为您没有初始化 mapKey 和 mapData 并且 getter 方法返回 null。

还将 getKeys() 方法更改为 getKey(),因为在 json 中,您的 json 键是键。

你的班级看起来像

public class UpdateInfo {

private Map<String, Object> mapKey = new HashMap<>();
private Map<String, Object> mapData = new HashMap<>();

ObjectMapper mapper;

public void setMapKey(JsonNode key) {
    this.mapKey = mapper.convertValue(key, new TypeReference<Map<String, Object>>() {
    });
}

public void setMapData(JsonNode data) {
    this.mapData = mapper.convertValue(data, new TypeReference<Map<String, Object>>() {
    });
}

public Map<String, Object> getKey() {
    return mapKey;
}

public Map<String, Object> getData() {
    return mapData;
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2021-09-09
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多