Search in sources :

Example 1 with DefaultErrorAttributes

use of org.springframework.boot.autoconfigure.web.DefaultErrorAttributes in project spring-boot-jpa by ssherwood.

the class BootJpaApplication method errorAttributes.

/**
 * Customized ErrorAttribute bean.
 * We really need to find a cleaner way of handling these error messages.
 *
 * @return customized ErrorAttributes
 */
@Bean
public ErrorAttributes errorAttributes() {
    return new DefaultErrorAttributes() {

        @Override
        public Map<String, Object> getErrorAttributes(final RequestAttributes requestAttributes, final boolean includeStackTrace) {
            Map<String, Object> attributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
            Throwable error = getError(requestAttributes);
            if (error instanceof MethodArgumentNotValidException) {
                MethodArgumentNotValidException ex = ((MethodArgumentNotValidException) error);
                attributes.put("errors", ex.getMessage());
            }
            return attributes;
        }
    };
}
Also used : RequestAttributes(org.springframework.web.context.request.RequestAttributes) DefaultErrorAttributes(org.springframework.boot.autoconfigure.web.DefaultErrorAttributes) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) Bean(org.springframework.context.annotation.Bean)

Aggregations

DefaultErrorAttributes (org.springframework.boot.autoconfigure.web.DefaultErrorAttributes)1 Bean (org.springframework.context.annotation.Bean)1 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)1 RequestAttributes (org.springframework.web.context.request.RequestAttributes)1