当我的请求出现问题时,我需要有自己的错误响应主体,我正在尝试使用 @NotEmpty
约束消息属性来返回错误消息,
这是我的类,它使用我需要的正文返回错误消息:
package c.m.nanicolina.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
@ControllerAdvice
public class CustomResponseEntityExceptionHandler {
@ExceptionHandler(value = {MissingServletRequestParameterException.class})
public ResponseEntity<ApiError> handleConflict(MissingServletRequestParameterException ex, WebRequest request) {
ApiError apiError = new ApiError(ex.getMessage(), ex.getMessage(), 1000);
return new ResponseEntity<ApiError>(apiError, null, HttpStatus.BAD_REQUEST);
}
}
有了这个 CustomResponseEntityExceptionHandler
,我可以在验证错误的情况下返回我自己的响应主体。
我现在正在尝试的是从验证约束中获取消息。
这是我的带有 NotEmpty
约束的 Controller :
package c.m.nanicolina.controllers;
import c.m.nanicolina.models.Product;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.constraints.NotEmpty;
@RestController
public class MinimumStockController {
@RequestMapping(value = "/minimumstock")
public Product product(
@RequestParam(value = "product.sku") @NotEmpty(message = "Product.sku cannot be empty") String sku,
@RequestParam(value = "stock.branch.id") String branchID) {
return null;
}
}
在我的异常(exception)情况下,我找不到获取该消息 Product.sku cannot be empty
并在我的错误响应中显示它的方法。
我还检查了类 MissingServletRequestParameterException
并且有返回默认消息的方法 getMessage
。
最佳答案
是的,它是可行的 & spring 很好地支持它。您只是缺少一些在 spring 中启用它的配置。
- Use Spring
@Validated
annotation to enable spring to validate controller- Handle
ConstraintViolationException
in yourControllerAdvice
to catch all failed validation messages.- Mark
required=false
in@RequestParam
, so it will not throw MissingServletRequestParameterException and rather move to next step of constraint validation.
@ControllerAdvice
public class CustomResponseEntityExceptionHandler {
@ExceptionHandler
public ResponseEntity<ApiError> handle(ConstraintViolationException exception) {
//you will get all javax failed validation, can be more than one
//so you can return the set of error messages or just the first message
String errorMessage = new ArrayList<>(exception.getConstraintViolations()).get(0).getMessage();
ApiError apiError = new ApiError(errorMessage, errorMessage, 1000);
return new ResponseEntity<ApiError>(apiError, null, HttpStatus.BAD_REQUEST);
}
}
@RestController
@Validated
public class MinimumStockController {
@RequestMapping(value = "/minimumstock")
public Product product(
@RequestParam(value = "product.sku", required=false) @NotEmpty(message = "Product.sku cannot be empty") String sku,
@RequestParam(value = "stock.branch.id", required=false) String branchID) {
return null;
}
}
注意 MissingServletRequestParameterException
将无法访问 javax 验证消息,因为它在请求生命周期中发生约束验证之前抛出。
关于java - Spring Boot 如何返回我自己的验证约束错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51331679/