0

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?

2
  • @Nemanja, thank you! You can add it as a response to accept it. Commented Jul 3, 2021 at 19:30
  • @Zion Ok, I added it as response :) Commented Jul 4, 2021 at 20:31

1 Answer 1

3

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())

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.