Ger*_*rep 1 java validation exception-handling query-parameters spring-boot
当我的请求出现问题时,我需要有自己的错误响应正文,并且我试图使用@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);
}
}
Run Code Online (Sandbox Code Playgroud)
这样,CustomResponseEntityExceptionHandler我就可以在验证错误的情况下返回自己的响应正文。
我现在正在尝试的是从验证约束中获取消息。
这是我的控制器的NotEmpty约束:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的例外中,我找不到获取该消息Product.sku cannot be empty并将其显示在错误响应中的方法。
我还检查了该类,MissingServletRequestParameterException并且有getMessage返回默认消息的方法。
是的,它是可行的,并且spring很好地支持了它。您只是缺少一些在春季启用它的配置。
- 使用Spring
@Validated注释启用spring来验证控制器- 处理
ConstraintViolationException您ControllerAdvice以捕获所有失败的验证消息。- 标记
required=false为@RequestParam,因此它不会引发MissingServletRequestParameterException,而是转到约束验证的下一步。
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
注意: MissingServletRequestParameterException将无法访问javax验证消息,因为它是在请求生命周期中进行约束验证之前抛出的。