我将一个对象(一个项目)从我的React前端传递给我的Spring Boot控制器,然后传递给验证项目的服务。 我的验证工作正常,并且后端一切正常,但是将验证响应从后端发送回前端时遇到问题。这是我的Spring控制器:
@RequestMapping(value="/validate", method = POST, produces = APPLICATION_JSON_VALUE)
public void validate(@RequestBody Project project, BindingResult result) {
projectsService.validate(project, result);
if(result.hasErrors()) {
throw new ValidationException("invalidProject", result);
}
}
这是我正在使用的控制器方法,并且我想要保持相同的结构(因为我不想返回任何东西,如果项目无效,我想抛出ValidationException并将其捕获在前端)
如您所见,我正在将bindingResult(其中包含所有验证信息)传递给我创建的ValidationException。
但是,当我在前端收到错误时,bindingResult消失了,唯一可见的是嵌套在通常错误代码中的Exception消息(invalidProject)。这是我指的响应:
<!DOCTYPE html><html><head><title>Apache Tomcat/8.0.32 - Error report</title><style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style> </head><body><h1>HTTP Status 500 - Request processing failed; nested exception is com.oreillyauto.pricingweb.ofmprojectadmin.commons.exceptions.ValidationException: invalidProject</h1><div class="line"></div><p><b>type</b> Exception report</p><p><b>message</b> <u>Request processing failed; nested exception is com.oreillyauto.pricingweb.ofmprojectadmin.commons.exceptions.ValidationException: invalidProject</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u></p><p><b>exception</b></p><pre>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.oreillyauto.pricingweb.ofmprojectadmin.commons.exceptions.ValidationException: invalidProject
所以就这样继续下去,然后是堆栈跟踪...这是我的问题。为什么Exception对象本身没有发送到前端?如果可能的话,它可能在哪里,因为我浏览了响应的每个单词,却找不到它。如果没有将对象发送到前端,是否有办法做到?
我在Spring的验证文档中发现的所有内容都旨在处理后端上的错误,或者仅在没有其他信息的情况下拒绝请求,但是我想在前端上显示相关的验证信息(即项目中缺少哪些字段) )。
这是我的前端Axios服务:
static ValidateProject(project) {
return axios.post(BASE_URL + 'project/validate/', project)
.then(response => response.data)
.catch(error => {
console.log(error.response.data)
ExceptionService.handle(error)
})
}
这是我的自定义例外:
public class ValidationException extends RuntimeException {
private static final long serialVersionUID = 3124218441365707050L;
private BindingResult bindingResult;
public ValidationException(String message, BindingResult bindingResult) {
super(message);
this.bindingResult = bindingResult;
}
public BindingResult getBindingResult() {
return bindingResult;
}
public List<ValidationErrorItem> getValidationErrors() {
List<ValidationErrorItem> errorItems = new ArrayList<>();
for (ObjectError next : bindingResult.getAllErrors()) {
String field = null;
if (next instanceof FieldError) {
FieldError fieldError = (FieldError) next;
field = fieldError.getField();
}
errorItems.add(new ValidationErrorItem(field, next.getCode(), next.getDefaultMessage()));
}
return errorItems;
}
}