我想在我的Rest spring引導應用程序中處理異常。我知道用@ControllerAdvice和ResponseEntity我可以返回一個代表我的錯誤的自定義對象,但我想要的是向exesting異常的主體添加一個新字段。在body rest中添加新字段spring rest
我創建了一個自定義異常繼承RuntimeException的一個額外的屬性,字符串列表:
@ResponseStatus(HttpStatus.CONFLICT)
public class CustomException extends RuntimeException {
private List<String> errors = new ArrayList<>();
public CustomException(List<String> errors) {
this.errors = errors;
}
public CustomException(String message) {
super(message);
}
public CustomException(String message, List<String> errors) {
super(message);
this.errors = errors;
}
public List<String> getErrors() {
return errors;
}
public void setErrors(List<String> errors) {
this.errors = errors;
}
}
在我的控制器我只是把這個自定義異常這樣:
@GetMapping("/appointment")
public List<Appointment> getAppointments() {
List<String> errors = new ArrayList<>();
errors.add("Custom message");
throw new CustomException("This is my message", errors);
}
當我測試我的休息端點郵遞員,好像春天開機不馬歇爾我的錯誤領域,迴應是:
{
"timestamp": "2017-06-05T18:19:03",
"status": 409,
"error": "Conflict",
"exception": "com.htech.bimaristan.utils.CustomException",
"message": "This is my message",
"path": "/api/agenda/appointment"
}
如果我可以從異常中獲取「路徑」和「時間戳」字段,但是沒有獲取這兩個屬性的getters,我可以使用@ControllerAdvice來定製對象。
謝謝。
我想這個功能在我的控制器整合,但我我做了我的端點調用每次得到這個異常: org.thymeleaf.exceptions.TemplateInputException:錯誤解析模板「通用/錯誤」,模板可能不存在或可能無法被配置的任何模板解析器訪問 – Habchi
我認爲您正在實現REST端點。所以,使用ResponseEntity。 – YuVi
這個解決方案就是我在問題中提到的。我可以使用它,但是如何從異常體中獲取路徑和時間戳屬性,以便我可以將它們添加到自定義錯誤對象? – Habchi