=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityAdvice.java' --- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityAdvice.java 2013-08-23 16:05:01 +0000 +++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityAdvice.java 2013-11-01 10:01:06 +0000 @@ -41,11 +41,14 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.web.HttpMediaTypeNotSupportedException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.HttpStatusCodeException; +import org.springframework.web.context.request.WebRequest; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; import java.io.IOException; @@ -53,13 +56,13 @@ * @author Morten Olav Hansen */ @ControllerAdvice -public class FacilityAdvice +public class FacilityAdvice extends ResponseEntityExceptionHandler { //-------------------------------------------------------------------------- // EXCEPTION HANDLERS //-------------------------------------------------------------------------- - @ExceptionHandler( { HttpClientErrorException.class, HttpServerErrorException.class } ) + @ExceptionHandler({ HttpClientErrorException.class, HttpServerErrorException.class }) public ResponseEntity statusCodeExceptionHandler( HttpStatusCodeException ex ) throws IOException { HttpHeaders headers = new HttpHeaders(); @@ -69,7 +72,7 @@ ex.getMessage() ), headers, ex.getStatusCode() ); } - @ExceptionHandler( { DeleteNotAllowedException.class, HierarchyViolationException.class } ) + @ExceptionHandler({ DeleteNotAllowedException.class, HierarchyViolationException.class }) public ResponseEntity handleForbidden( Exception ex ) throws IOException { HttpHeaders headers = new HttpHeaders(); @@ -89,7 +92,7 @@ ex.getMessage() ), headers, HttpStatus.PRECONDITION_FAILED ); } - @ExceptionHandler( { FacilityNotFoundException.class } ) + @ExceptionHandler({ FacilityNotFoundException.class }) public ResponseEntity handleNotFound( Exception ex ) throws IOException { HttpHeaders headers = new HttpHeaders(); @@ -99,7 +102,7 @@ ex.getMessage() ), headers, HttpStatus.NOT_FOUND ); } - @ExceptionHandler( { DuplicateCodeException.class, DuplicateUidException.class, DuplicateUuidException.class } ) + @ExceptionHandler({ DuplicateCodeException.class, DuplicateUidException.class, DuplicateUuidException.class }) public ResponseEntity handleConflict( Exception ex ) throws IOException { HttpHeaders headers = new HttpHeaders(); @@ -108,4 +111,13 @@ return new ResponseEntity( MessageUtils.jsonMessage( HttpStatus.CONFLICT.toString(), ex.getMessage() ), headers, HttpStatus.CONFLICT ); } + + @Override + protected ResponseEntity handleHttpMediaTypeNotSupported( HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request ) + { + headers.add( "Content-Type", MediaType.APPLICATION_JSON_VALUE ); + + return new ResponseEntity( MessageUtils.jsonMessage( + "415", ex.getMessage() ), HttpStatus.UNSUPPORTED_MEDIA_TYPE ); + } } === modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/MessageUtils.java' --- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/MessageUtils.java 2013-08-23 16:05:01 +0000 +++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/MessageUtils.java 2013-11-01 10:01:06 +0000 @@ -49,19 +49,28 @@ objectMapper.setSerializationInclusion( JsonInclude.Include.NON_EMPTY ); } - public static String jsonMessage( String message ) throws IOException + public static String jsonMessage( String message ) { return messageToJson( new MessageResponse( null, message ) ); } - public static String jsonMessage( String code, String message ) throws IOException + public static String jsonMessage( String code, String message ) { return messageToJson( new MessageResponse( code, message ) ); } - public static String messageToJson( MessageResponse messageResponse ) throws IOException + public static String messageToJson( MessageResponse messageResponse ) { - return objectMapper.writeValueAsString( messageResponse ); + try + { + return objectMapper.writeValueAsString( messageResponse ); + } + catch ( IOException e ) + { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + + throw new IllegalArgumentException(); } private MessageUtils()