0

将这两种方法合二为一的最佳方法是什么?

@RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<String> dbStatus() {
        return statusService.isDbAlive() ? RESPONSE_DB_UP : RESPONSE_DB_DOWN;
    }

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<String> appStatus() throws IOException {
        return statusService.isAppAlive() ? RESPONSE_APP_UP : RESPONSE_APP_DOWN;
    }

我可以返回一个ListResponseEntity<String>

4

2 回答 2

1

您必须返回类似 JSON 的内容,以显示两项检查的结果。例如

{"isAppAlive" : true, "isDbAlive": false}

您可以自己构建这个 JSON 字符串

@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<String> getStatus() throws IOException {
    String json = "{\"isAppAlive\":" + statusService.isAppAlive() + ",\"isDbAlive\""+ statusService.isDbAlive() +"}";
    return new ResponseEntity<String>(json, HttpStatus.OK);
}

或者建立一个像

public class Status {
    // use private and getters/setters
    public boolean isAppAlive;
    public boolean isDbAlive; 
}

并让 Spring 序列化Status您创建的对象

@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Status> getStatus() throws IOException {
    Status status = new Status();
    status.isAppAlive = statusService.isAppAlive();
    status.isDbAlive = statusService.isDbAlive();
    return new ResponseEntity<Status>(status, HttpStatus.OK);
}

您总是可以创建statusService一个方法getStatus()来返回一个Status已经设置了其字段的对象。

然后,您的客户端可以解析 JSON 并检查每个状态。

于 2013-08-29T15:25:44.637 回答
0
  @RequestMapping(value = "/testurl",
       method = { RequestMethod.GET, RequestMethod.POST })
  public ModelAndView dbStatus() {

             List<String>  status = new ArrayList<String>();
             status.add (statusService.isDbAlive() );
             status.add (statusService.isAppAlive() );
             ModelMap modelMap = new ModelMap();
             modelMap.put("status ", status );
             return new ModelAndView("statuspage.jsp", modelMap);

  }
于 2013-08-29T15:30:44.503 回答