Java tutorial
/* * VisualSync - a tool to visualize user data synchronization * Copyright (c) 2016 Gary Clayburg * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package com.garyclayburg.persistence.repository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.validation.FieldError; import org.springframework.validation.ObjectError; import org.springframework.web.HttpMediaTypeNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; import java.util.ArrayList; import java.util.List; /** * Created by IntelliJ IDEA. * Date: 4/21/16 * Time: 2:48 PM * https://www.jayway.com/2013/02/03/improve-your-spring-rest-api-part-iii/ * * @author Gary Clayburg */ @ControllerAdvice public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { @SuppressWarnings("UnusedDeclaration") private static final Logger log = LoggerFactory.getLogger(CustomResponseEntityExceptionHandler.class); @Override protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors(); List<ObjectError> globalErrors = ex.getBindingResult().getGlobalErrors(); List<String> errors = new ArrayList<>(fieldErrors.size() + globalErrors.size()); String error; for (FieldError fieldError : fieldErrors) { error = fieldError.getField() + ", " + fieldError.getDefaultMessage(); errors.add(error); } for (ObjectError objectError : globalErrors) { error = objectError.getObjectName() + ", " + objectError.getDefaultMessage(); errors.add(error); } ErrorMessage errorMessage = new ErrorMessage(errors); return new ResponseEntity(errorMessage, headers, status); } @Override protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { String unsupported = "Unsupported content type: " + ex.getContentType(); String supported = "Supported content types: " + MediaType.toString(ex.getSupportedMediaTypes()); ErrorMessage errorMessage = new ErrorMessage(unsupported, supported); return new ResponseEntity(errorMessage, headers, status); } @Override protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { Throwable mostSpecificCause = ex.getMostSpecificCause(); ErrorMessage errorMessage; if (mostSpecificCause != null) { String exceptionName = mostSpecificCause.getClass().getName(); String message = mostSpecificCause.getMessage(); errorMessage = new ErrorMessage(exceptionName, message); } else { errorMessage = new ErrorMessage(ex.getMessage()); } return new ResponseEntity(errorMessage, headers, status); } }