Skip to content
Advertisement

JsonObject returns {“empty”:false} in API response

I have the following code on my project:

@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;
        
    }

}

When I Log htmlcda parameter, I get the html string correctly. However, in response results JsonObject I get {“empty”:false}. Any help please?

Advertisement

Answer

Spring boot uses jackson as default serializer and you’re trying to return JSONObject itself. Jackson does not know how to serialize it. If you need to return JSON, you can put your properties in Map, and return it. Otherwise, you can return results.toString(), or ResponseEntity.status(HttpStatus.OK).body(result.toString())

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement