Spring Rest Service在第1行的第1行调用返回错误:文档为空

jun*_*sal 5 java rest google-chrome spring-mvc spring-boot

我写了一个REST调用,它会在调用时返回健康状态

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

}
Run Code Online (Sandbox Code Playgroud)

当我以招摇或邮差打开它时,它会返回正确的响应.但是,当我在Chrome浏览器中点击此URL时,我看到了

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.
Run Code Online (Sandbox Code Playgroud)

为什么这样?以及如何解决这个问题?

小智 -1

尝试返回的不是字符串,而是

return new ResponseEntity<>(yourString, HttpStatus.OK);
Run Code Online (Sandbox Code Playgroud)

并且也改变这个

public @ResponseBody String getHealth(HttpServletRequest request, HttpServletResponse response) throws Exception {
Run Code Online (Sandbox Code Playgroud)

对此

public @ResponseBody ResponseEntity<String> getHealth(HttpServletRequest request, HttpServletResponse response) throws Exception {
Run Code Online (Sandbox Code Playgroud)

如果它不起作用,请尝试在浏览器中访问 URL 时将 .xml 或 .json 添加到 URL 末尾。