Spring Rest Service Return call error in row 1 in column 1: Document is empty

I wrote a REST Call that will return the health status on call

@RestController @RequestMapping(value = "/account") public class HealthCheckController { protected final Logger log = LoggerFactory.getLogger(this.getClass()); @RequestMapping(value = "/health", method = RequestMethod.GET, produces = { "application/json" }) @ResponseStatus(HttpStatus.OK) @ApiOperation(value = "Returns the health status of the application", notes = "Load balancer user this to confirm the health of the node") public @ResponseBody String getHealth(HttpServletRequest request, HttpServletResponse response) throws Exception { log.info("***" + RequestCorrelation.getId() + "***" + "HealthCheckController - getHealth () Called"); return "{\"health\":{\"SERVICES\":\"OK\"}}"; } } 

When I open this in swagger or postman, it returns the correct answer. But when I hit this url in Chrome browser, I see

 This page contains the following errors: error on line 1 at column 1: Document is empty Below is a rendering of the page up to the first error. 

Why is that? and how to fix it?

+5
source share
1 answer

Try to return not a string, but

 return new ResponseEntity<>(yourString, HttpStatus.OK); 

also change this value

 public @ResponseBody String getHealth(HttpServletRequest request, HttpServletResponse response) throws Exception { 

to that

 public @ResponseBody ResponseEntity<String> getHealth(HttpServletRequest request, HttpServletResponse response) throws Exception { 

And if that doesn't work, try adding .xml or .json to the end of your URL when accessing it in a browser.

0
source

Source: https://habr.com/ru/post/1247675/


All Articles