KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have the requirement to return the result from the database either as a string in xml-structure or as json-structure. I've got a solution, but I don't know, if this one is the best way to solve this. I have two methods here: @RequestMapping(value = "/content/json/{ids}", method = RequestMethod.GET) public ResponseEntity<String> getContentByIdsAsJSON(@PathVariable("ids") String ids) { String content = null; StringBuilder builder = new StringBuilder(); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.add("Content-Type", "text/html; charset=utf-8"); // responseHeaders.add("Content-Type", "application/json; charset=utf-8"); List<String> list = this.contentService.findContentByListingIdAsJSON(ids); if (list.isEmpty()) { content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><error>no data found</error>"; return new ResponseEntity<String>(content, responseHeaders, HttpStatus.CREATED); } for (String json : list) { builder.append(json + "\n"); } content = builder.toString(); return new ResponseEntity<String>(content, responseHeaders, HttpStatus.CREATED); } @RequestMapping(value = "/content/{ids}", method = RequestMethod.GET) public ResponseEntity<String> getContentByIdsAsXML(@PathVariable("ids") String ids) { HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.add("Content-Type", "application/xml; charset=utf-8"); String content = this.contentService.findContentByListingIdAsXML(ids); if (content == null) { content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><error>no data found</error>"; return new ResponseEntity<String>(content, responseHeaders, HttpStatus.CREATED); } return new ResponseEntity<String>(content, responseHeaders, HttpStatus.CREATED); } for the first method I need a better solution, which I already asked here: <a href="https://stackoverflow.com/qu
Tags (comma-separated)
Save Edits
Cancel