1
我有一個名爲handleImageBannerUpload的方法。在這個方法中,我有一個switch語句。有3個開關的情況和每個人都應該返回不同的ResponseEntity:如何在交換機中使用ResponseEntity?
switch(type){
case "localEvent":
try {
LocalEvent localEvent = localEventRepository.findOne(id);
storageService.store(file, localEvent);
String path = localEvent.getBannerPath();
log("FileUploadController: " + path);
result = ResponseEntity
.ok()
.header(HttpHeaders.CONTENT_TYPE, "application/json;")
.body(path);
}
catch (StorageException e) {
result = ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.header(HttpHeaders.CONTENT_TYPE, "application/json;")
.body("\"" + e.getMessage() + "\"");
}
case "session":
try {
Session session = sessionRepository.findOne(id);
storageService.store(file, session);
String path = session.getSessionDescriptionImagePath();
log("FileUploadController: " + path);
result = ResponseEntity
.ok()
.header(HttpHeaders.CONTENT_TYPE, "application/json;")
.body(path);
}
catch (StorageException e) {
result = ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.header(HttpHeaders.CONTENT_TYPE, "application/json;")
.body("\"" + e.getMessage() + "\"");
}
case "theme":
try{
Theme theme = themeRepository.findOne(id);
storageService.store(file, theme);
String path = theme.getThemeImagePath();
log("FileUploadController: " + path);
result = ResponseEntity
.ok()
.header(HttpHeaders.CONTENT_TYPE, "application/json;")
.body(path);
}
catch (StorageException e) {
result = ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.header(HttpHeaders.CONTENT_TYPE, "application/json;")
.body("\"" + e.getMessage() + "\"");
}
}
return result;
如何創建一個適當的result
變量,可以在switch語句返回ResponseEntity?