java – 在body exception spring rest中添加新字段

前端之家收集整理的这篇文章主要介绍了java – 在body exception spring rest中添加新字段前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在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;
    }
}

在我的控制器中,我只是这样抛出这个自定义异常:

@GetMapping("/appointment")
public List<Appointment> getAppointments() {
    List<String> errors = new ArrayList<>();
    errors.add("Custom message");
    throw new CustomException("This is my message",errors);
}

当我用邮递员测试我的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"
}

我可以使用@ControllerAdvice来获取自定义对象,如果我可以从异常中获取“path”和“timestamp”字段,但是这两个属性没有getter.

谢谢.

解决方法

好!以下是DefaultErrorAttributes中“path”和“timestamp”的实现,您也可以在自定义实现中执行此操作:

路径:

String path = getAttribute(requestAttributes,"javax.servlet.error.request_uri");
if (path != null) {
    errorAttributes.put("path",path);
}

时间戳:

errorAttributes.put("timestamp",new Date());

弹簧启动时错误自定义的文档是here.

@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;
        }

   };
}

或者您可以编写自定义实现:

@Component
public class CustomErrorAttributes extends DefaultErrorAttributes {

    @Override
    public Map<String,boolean includeStackTrace) {
        Map<String,includeStackTrace);
        // customize here
        return errorAttributes;
    }
}

ErrorAttributes bean自定义下面的错误响应:

{
   "timestamp": 1413883870237,"status": 500,"error": "Internal Server Error","exception": "org.example.ServiceException","message": "somthing goes wrong","path": "/index"
}

可以使用@ExceptionHandler自定义“exception”属性. @ControlerAdvice可用于跨控制器一般自定义异常.要在Controller级别进行自定义,可以将它们放在控制器中.

在你的情况下:

@ResponseStatus(value=HttpStatus.BAD_REQUEST,reason="Invalid Inputs")
    @ExceptionHandler(CustomException.class)
    private void errorHanlder() {
        //Log exception
    }


  public Map<String,boolean includeStackTrace) {
    Map<String,includeStackTrace);
    Throwable error = getError(requestAttributes);
    if (error instanceof CustomException) {
        errorAttributes.put("errorList",((CustomException)error).getErrors());
    }
    return errorAttributes;
}
原文链接:https://www.f2er.com/java/121535.html

猜你在找的Java相关文章