Hab*_*chi 6 java rest spring exception-handling spring-boot
我想在Rest spring启动应用程序中处理异常.我知道使用@ControllerAdvice和ResponseEntity,我可以返回一个代表我的错误的自定义对象,但我想要的是在exesting异常的主体中添加一个新字段.
我创建了一个自定义的Exception,它继承了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;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我只是这样抛出这个自定义异常:
@GetMapping("/appointment")
public List<Appointment> getAppointments() {
List<String> errors = new ArrayList<>();
errors.add("Custom message");
throw new CustomException("This is my message", errors);
}
Run Code Online (Sandbox Code Playgroud)
当我用邮递员测试我的Rest端点时,似乎spring boot不会编组我的错误字段,响应是:
{
"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"
}
Run Code Online (Sandbox Code Playgroud)
我可以使用@ControllerAdvice来获取自定义对象,如果我可以从异常中获取"path"和"timestamp"字段,但是这两个属性没有getter.
谢谢.
好!以下是DefaultErrorAttributes中"path"和"timestamp"的实现,您也可以在自定义实现中执行此操作:
路径:
String path = getAttribute(requestAttributes, "javax.servlet.error.request_uri");
if (path != null) {
errorAttributes.put("path", path);
}
Run Code Online (Sandbox Code Playgroud)
时间戳:
errorAttributes.put("timestamp", new Date());
Run Code Online (Sandbox Code Playgroud)
弹簧启动中的错误自定义文档就在这里.
@Bean
public ErrorAttributes errorAttributes() {
return new DefaultErrorAttributes() {
@Override
public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
// customize here
return errorAttributes;
}
};
}
Run Code Online (Sandbox Code Playgroud)
或者您可以编写自定义实现:
@Component
public class CustomErrorAttributes extends DefaultErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
// customize here
return errorAttributes;
}
}
Run Code Online (Sandbox Code Playgroud)
该ErrorAttributes豆定制的错误如下回应:
{
"timestamp": 1413883870237,
"status": 500,
"error": "Internal Server Error",
"exception": "org.example.ServiceException",
"message": "somthing goes wrong",
"path": "/index"
}
Run Code Online (Sandbox Code Playgroud)
该"exception"属性可以使用@ExceptionHandler.ControlerAdvice可以使用@ 来跨控制器一般自定义异常.要在Controller级别进行自定义,可以将它们放在控制器中.
在你的情况下:
@ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="Invalid Inputs")
@ExceptionHandler(CustomException.class)
private void errorHanlder() {
//Log exception
}
public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
Throwable error = getError(requestAttributes);
if (error instanceof CustomException) {
errorAttributes.put("errorList", ((CustomException)error).getErrors());
}
return errorAttributes;
}
Run Code Online (Sandbox Code Playgroud)
以前的答案确实包含了所有内容,但不知何故我花了一段时间才弄明白,所以总的来说,实现这一点的最简单方法是拥有一个这样的 bean:
@Bean
public ErrorAttributes errorAttributes() {
return new DefaultErrorAttributes() {
@Override
public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes,
boolean includeStackTrace) {
Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
Throwable error = getError(requestAttributes);
if (error instanceof CustomExceptionthere) {
errorAttributes.put("errorList", ((CustomException)error).getErrors());
}
return errorAttributes;
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3533 次 |
| 最近记录: |