What's the best method for converting these two methods into one?
@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;
}
Could I return a List
of ResponseEntity<String>
?
You would have to return something like JSON showing the result of both checks. For example
{"isAppAlive" : true, "isDbAlive": false}
You can build this JSON string yourself
@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);
}
Or build a class like
public class Status {
// use private and getters/setters
public boolean isAppAlive;
public boolean isDbAlive;
}
and let Spring serialize the Status
object you create
@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);
}
You can always make statusService
have a method getStatus()
that returns a Status
object that already has its fields set already.
Your client can then parse the JSON and check each status.
@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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.