gpt4 book ai didi

java - 在正文异常 spring rest 中添加新字段

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:01:01 25 4
gpt4 key购买 nike

我想在我的 Rest spring boot 应用程序中处理异常。我知道使用 @ControllerAdvice 和 ResponseEntity 我可以返回一个自定义对象来表示我的错误,但我想要的是向现有异常的主体添加一个新字段,仅此而已。

我创建了一个继承 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;
}
}

在我的 Controller 中,我只是以这种方式抛出这个自定义异常:

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

当我用 postman 测试我的 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 获取自定义对象,但这两个属性没有 getter 。

谢谢。

最佳答案

嗯!以下是 DefaultErrorAttributes 中“路径”和“时间戳”的实现,您也可以在自定义实现中实现:

路径:

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

时间戳:

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

spring boot中错误定制的文档是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, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, 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 自定义异常。要在 Controller 级别进行自定义,您可以将它们放在 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;
}

关于java - 在正文异常 spring rest 中添加新字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44375456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com