Search in sources :

Example 1 with ValidationError

use of com.breuninger.arch.playground.common.domain.ValidationError in project arch-playground by BeneStem.

the class ExceptionHandlerHtmlControllerAdvice method handleConstraintViolation.

@ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ModelAndView handleConstraintViolation(final ConstraintViolationException exception) {
    final MappingJackson2JsonView view = new CustomMappingJackson2JsonView();
    final var modelAndView = new ModelAndView(view);
    modelAndView.addObject(exception.getConstraintViolations().stream().map(constraintViolation -> new ValidationError(((PathImpl) constraintViolation.getPropertyPath()).getLeafNode().getName(), constraintViolation.getMessage())).collect(toList()));
    return modelAndView;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) ValidationError(com.breuninger.arch.playground.common.domain.ValidationError) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus)

Example 2 with ValidationError

use of com.breuninger.arch.playground.common.domain.ValidationError in project arch-playground by BeneStem.

the class ExceptionHandlerHtmlControllerAdvice method handleBadRequest.

// @ExceptionHandler(RedirectionException.class)
// public RedirectView redirection(final RedirectionException exception) {
// final RedirectView loginRedirectView = new RedirectView(exception.getLocation().toString());
// loginRedirectView.setStatusCode(HttpStatus.valueOf(exception.getResponse().getStatus()));
// loginRedirectView.setExposeModelAttributes(false);
// return loginRedirectView;
// }
@ExceptionHandler(BadRequestException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ModelAndView handleBadRequest(final BadRequestException exception) {
    final MappingJackson2JsonView view = new CustomMappingJackson2JsonView();
    final var modelAndView = new ModelAndView(view);
    modelAndView.addObject(exception.getErrors().getFieldErrors().stream().map(fieldError -> new ValidationError(fieldError.getField(), fieldError.getDefaultMessage())).collect(toList()));
    return modelAndView;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) ValidationError(com.breuninger.arch.playground.common.domain.ValidationError) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus)

Example 3 with ValidationError

use of com.breuninger.arch.playground.common.domain.ValidationError in project arch-playground by BeneStem.

the class ExceptionHandlerHtmlControllerAdvice method handleMethodArgumentNotValid.

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
protected ModelAndView handleMethodArgumentNotValid(final MethodArgumentNotValidException exception) {
    final MappingJackson2JsonView view = new CustomMappingJackson2JsonView();
    final var modelAndView = new ModelAndView(view);
    modelAndView.addObject(exception.getBindingResult().getFieldErrors().stream().map(fieldError -> new ValidationError(fieldError.getField(), fieldError.getDefaultMessage())).collect(toList()));
    return modelAndView;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) ValidationError(com.breuninger.arch.playground.common.domain.ValidationError) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus)

Aggregations

ValidationError (com.breuninger.arch.playground.common.domain.ValidationError)3 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)3 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)3 ModelAndView (org.springframework.web.servlet.ModelAndView)3 MappingJackson2JsonView (org.springframework.web.servlet.view.json.MappingJackson2JsonView)3