【问题标题】:JsonObject returns {"empty":false} in API responseJsonObject 在 API 响应中返回 {"empty":false}
【发布时间】:2021-07-02 13:58:26
【问题描述】:

我的项目中有以下代码:

@SessionScope
@RestController
public class cdaDisplayTool {
    private final static Logger LOG = LoggerFactory.getLogger(cdaDisplayTool.class);

    @PostMapping(path = "/display_XML", produces = "application/json; charset=utf-8")
    protected JSONObject display_XML(@RequestBody ObjectNode model,HttpServletRequest request, 
            HttpServletResponse response) throws ServletException, IOException {
        
            String cda_base64 = null;
            String htmlcda = null;
            JSONObject results = new JSONObject();
            
            if (model.has("cda_base64")) {
                cda_base64 = model.get("cda_base64").asText();
                byte[] decodedBytes = Base64.getDecoder().decode(cda_base64);
                String decodedString = new String(decodedBytes);
                String cda_xml = decodedString;
                htmlcda = CdaXSLTransformer.getInstance().transform(cda_xml, "en-GB",
                        "/dispense");
            }
            
            LOG.info("cdahtml: "+ htmlcda);
            results.accumulate("status", 200).accumulate("data", htmlcda);
            return results;
        
    }

}

当我记录htmlcda 参数时,我正确获取了 html 字符串。然而,作为对resultsJsonObject 的回应,我得到了 {"empty":false}。有什么帮助吗?

【问题讨论】:

  • @Nemanja,谢谢!您可以将其添加为接受它的响应。
  • @Zion 好的,我将其添加为响应 :)

标签: java json spring-boot


【解决方案1】:

Spring boot 使用 jackson 作为默认序列化程序,您正在尝试返回 JSONObject 本身。 Jackson 不知道如何序列化它。如果你需要返回 JSON,你可以把你的属性放在Map,然后返回。否则,您可以返回results.toString(),或ResponseEntity.status(HttpStatus.OK).body(result.toString())

【讨论】:

    猜你喜欢
    • 2019-07-08
    • 2023-04-01
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多