【问题标题】:Spring Rest Service Call returning error on line 1 at column 1: Document is emptySpring Rest 服务调用在第 1 列的第 1 行返回错误:文档为空
【发布时间】:2016-08-15 12:30:15
【问题描述】:

我写了一个 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\"}}";
    }

}

当我在 swagger 或 postman 中打开它时,它会返回正确的响应。但是当我在 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.

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

【问题讨论】:

  • 嗨,shiva,我也遇到了同样的问题,它在本地工作,但在服务器上它停止了,你能告诉我这个问题与 Apache 服务器有关吗

标签: java rest google-chrome spring-mvc spring-boot


【解决方案1】:

在您的 getHealth() 方法中,您将返回一个字符串,但在您的 @RequestMapping 注释中,您指定您的方法将生成 JSON。

尝试以下方法之一:


 @RequestMapping(value = "/health", method = RequestMethod.GET, produces = { "text/plain" })
 //Now, pass Accept = "text/plain" in the request header:

  @RequestMapping(method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
  public List<String> getHealth(..) {
      /*
...

     */
   
            ArrayList<String> list=new ArrayList();
            list.add("Health OK");
            return list;

  
  }

这会给你 ["Health OK"] 在回复中。

【讨论】:

    【解决方案2】:

    有同样的问题。拥有一个具有以下类注解和方法的对象:

    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})

    @GET
    @Path("version")
    public String getVersion() { return "v1"; }
    

    将 MediaType.TEXT_PLAIN 添加到 @Produces 注释的末尾。没用。 将其移至 @Produces 注释的开头。没用。

    将它移动/添加到方法中为我解决了这个问题。您的客户也需要能够接受该媒体类型。

    @GET
    @Path("version")
    @Produces({MediaType.TEXT_PLAIN})
    public String getVersion() { return "v1"; )
    

    HTH

    【讨论】:

      【解决方案3】:

      尝试返回不是字符串而是

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

      也改变这个

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

      到这里

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

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

      【讨论】:

        猜你喜欢
        • 2011-04-03
        • 1970-01-01
        • 2021-11-08
        • 2023-01-27
        • 1970-01-01
        • 2021-02-03
        • 1970-01-01
        • 2011-12-18
        • 1970-01-01
        相关资源
        最近更新 更多