代码之家  ›  专栏  ›  技术社区  ›  Cody Pritchard

没有双重序列化的Spring-Return原始JSON

  •  1
  • Cody Pritchard  · 技术社区  · 7 年前

    我知道还有其他类似的帖子,但我还没有找到任何能帮助我找到解决这个特殊情况的方法。

    我正试着归还一张支票 HashMap<String, Object> 从我的控制器。 这个 Object

    控制器功能:

    @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public HashMap<String, Object> heartbeat(){
        String streamInfo = service.getStreamInfo();
        String streamCursorInfo = service.getStreamCursorInfo();
        String topicInfo = service.getTopicInfo();
    
        String greeting = "This is a sample app for using Spring Boot with MapR Streams.";
    
        HashMap<String, Object> results = new HashMap();
        results.put("greeting", greeting);
        results.put("streamInfo", streamInfo);
        results.put("streamCursorInfo", streamCursorInfo);
        results.put("topicInfo", topicInfo);
    
        return results;
    }
    

    服务功能:

    private String performCURL(String[] command){
        StringBuilder stringBuilder = new StringBuilder();
    
        try{
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process p = processBuilder.start();
    
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            while((line = reader.readLine()) != null){
                stringBuilder.append(line);
            }
        }
        catch(Exception e){
            LOGGER.error(ExceptionUtils.getRootCauseMessage(e));
        }
    
        return stringBuilder.toString();
    }
    

    我运行的cURL命令已经返回了一个原始的JSON字符串。所以我只是想把它添加到HashMap中,在heartbeat响应中返回。

    {
    "greeting": "This is a sample app for using Spring Boot with MapR Streams.",
    "streamCursorInfo": "{\"timestamp\":1538676344564,\"timeofday\":\"2018-10-04 02:05:44.564 GMT-0400 PM\",\"status\":\"OK\",\"total\":1,\"data\":[{\"consumergroup\":\"MapRDBConsumerGroup\",\"topic\":\"weightTags\",\"partitionid\":\"0\",\"produceroffset\":\"44707\",\"committedoffset\":\"10001\",\"producertimestamp\":\"2018-10-03T05:57:27.128-0400 PM\",\"consumertimestamp\":\"2018-09-21T12:35:51.654-0400 PM\",\"consumerlagmillis\":\"1056095474\"}]}",
    ...
    }
    

    streamInfo

    有人能解释我遗漏了什么或者需要做什么来防止双重序列化吗?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Wim Deblauwe    7 年前

    而不是返回 HashMap ,创建如下对象:

    public class HeartbeatResult {
      private String greeting;
      ... //other fields here
    
      @JsonRawValue
      private String streamCursorInfo;
    
      ... //getters and setters here (or make the object immutable by having just a constructor and getters)
    }
    

    @JsonRawValue 杰克逊将按原样序列化字符串。看到了吗 https://www.baeldung.com/jackson-annotations 更多信息。

        2
  •  0
  •   Adina Rolea    7 年前

    streamCursorInfo是字符串,而不是对象=>序列化将转义“字符”。

    ObjectMapper objectMapper = new ObjectMapper();
    
    JsonNode streamCursorInfo = objectMapper.readTree(service.getStreamInfo())
    
    results.put("streamCursorInfo", streamCursorInfo);