Search in sources :

Example 26 with BindingResult

use of org.springframework.validation.BindingResult in project entando-core by entando.

the class RestExceptionHandler method processValidationError.

@ExceptionHandler(value = ValidationGenericException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorRestResponse processValidationError(ValidationGenericException ex) {
    logger.debug("Handling {} error", ex.getClass().getSimpleName());
    BindingResult result = ex.getBindingResult();
    return processAllErrors(result);
}
Also used : BindingResult(org.springframework.validation.BindingResult) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 27 with BindingResult

use of org.springframework.validation.BindingResult in project entando-core by entando.

the class RestExceptionHandler method processValidationError.

@ExceptionHandler(value = ValidationUpdateSelfException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
@ResponseBody
public ErrorRestResponse processValidationError(ValidationUpdateSelfException ex) {
    logger.debug("Handling {} error", ex.getClass().getSimpleName());
    BindingResult result = ex.getBindingResult();
    return processAllErrors(result);
}
Also used : BindingResult(org.springframework.validation.BindingResult) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 28 with BindingResult

use of org.springframework.validation.BindingResult in project tutorials by eugenp.

the class ExceptionTranslator method processValidationError.

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorVM processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    return processFieldErrors(fieldErrors);
}
Also used : BindingResult(org.springframework.validation.BindingResult) FieldError(org.springframework.validation.FieldError)

Example 29 with BindingResult

use of org.springframework.validation.BindingResult in project tutorials by eugenp.

the class ExceptionTranslator method processValidationError.

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorVM processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    return processFieldErrors(fieldErrors);
}
Also used : BindingResult(org.springframework.validation.BindingResult) FieldError(org.springframework.validation.FieldError)

Example 30 with BindingResult

use of org.springframework.validation.BindingResult in project cetc by DiscoverForever.

the class ExceptionTranslator method handleMethodArgumentNotValid.

@Override
public ResponseEntity<Problem> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, @Nonnull NativeWebRequest request) {
    BindingResult result = ex.getBindingResult();
    List<FieldErrorVM> fieldErrors = result.getFieldErrors().stream().map(f -> new FieldErrorVM(f.getObjectName(), f.getField(), f.getCode())).collect(Collectors.toList());
    Problem problem = Problem.builder().withType(ErrorConstants.CONSTRAINT_VIOLATION_TYPE).withTitle("Method argument not valid").withStatus(defaultConstraintViolationStatus()).with("message", ErrorConstants.ERR_VALIDATION).with("fieldErrors", fieldErrors).build();
    return create(ex, problem, request);
}
Also used : ControllerAdvice(org.springframework.web.bind.annotation.ControllerAdvice) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) BindingResult(org.springframework.validation.BindingResult) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ProblemHandling(org.zalando.problem.spring.web.advice.ProblemHandling) Collectors(java.util.stream.Collectors) DefaultProblem(org.zalando.problem.DefaultProblem) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) List(java.util.List) Problem(org.zalando.problem.Problem) ProblemBuilder(org.zalando.problem.ProblemBuilder) Status(org.zalando.problem.Status) ConstraintViolationProblem(org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) HeaderUtil(com.cetc.cctv.web.rest.util.HeaderUtil) ResponseEntity(org.springframework.http.ResponseEntity) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) BindingResult(org.springframework.validation.BindingResult) DefaultProblem(org.zalando.problem.DefaultProblem) Problem(org.zalando.problem.Problem) ConstraintViolationProblem(org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem)

Aggregations

BindingResult (org.springframework.validation.BindingResult)140 Test (org.junit.Test)28 Test (org.junit.jupiter.api.Test)27 List (java.util.List)23 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)23 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)23 ResponseEntity (org.springframework.http.ResponseEntity)20 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)20 Collectors (java.util.stream.Collectors)18 HttpServletRequest (javax.servlet.http.HttpServletRequest)18 FieldError (org.springframework.validation.FieldError)16 ObjectError (org.springframework.validation.ObjectError)16 Model (org.springframework.ui.Model)15 ControllerAdvice (org.springframework.web.bind.annotation.ControllerAdvice)14 HashMap (java.util.HashMap)13 Nonnull (javax.annotation.Nonnull)13 Nullable (javax.annotation.Nullable)13 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)13 DefaultProblem (org.zalando.problem.DefaultProblem)13 Problem (org.zalando.problem.Problem)13