@Test public void readWithTypeMismatchException() throws Exception { MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]); Marshaller marshaller = mock(Marshaller.class); Unmarshaller unmarshaller = mock(Unmarshaller.class); given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(Integer.valueOf(3)); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller, unmarshaller); try { converter.read(String.class, inputMessage); fail("Should have thrown HttpMessageNotReadableException"); } catch (HttpMessageNotReadableException ex) { assertTrue(ex.getCause() instanceof TypeMismatchException); } }
@Test public void readWithMarshallingFailureException() throws Exception { MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]); UnmarshallingFailureException ex = new UnmarshallingFailureException("forced"); Unmarshaller unmarshaller = mock(Unmarshaller.class); given(unmarshaller.unmarshal(isA(StreamSource.class))).willThrow(ex); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); converter.setUnmarshaller(unmarshaller); try { converter.read(Object.class, inputMessage); fail("HttpMessageNotReadableException should be thrown"); } catch (HttpMessageNotReadableException e) { assertTrue("Invalid exception hierarchy", e.getCause() == ex); } }
@Override protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { String mensagemUsuario = messageSource.getMessage("mensagem.invalida", null, LocaleContextHolder.getLocale()); String mensagemDesenvolvedor = ex.getCause() != null ? ex.getCause().toString() : ex.toString(); List<Erro> erros = Arrays.asList(new Erro(mensagemUsuario, mensagemDesenvolvedor)); return handleExceptionInternal(ex, erros, headers, HttpStatus.BAD_REQUEST, request); }
@ExceptionHandler(HttpMessageNotReadableException.class) @ResponseStatus(value = HttpStatus.BAD_REQUEST) @ResponseBody public XApiErrorInfo handleHttpMessageNotReadableException(final HttpServletRequest request, HttpMessageNotReadableException e) { if (e.getCause() instanceof UnrecognizedPropertyException) { return this.handleUnrecognizedPropertyException(request, (UnrecognizedPropertyException)e.getCause()); } else { XApiErrorInfo result; if (e.getCause() instanceof JsonProcessingException) { final JsonProcessingException jpe = (JsonProcessingException)e.getCause(); result = new XApiErrorInfo(HttpStatus.BAD_REQUEST, request, jpe.getOriginalMessage()); } else { result = new XApiErrorInfo(HttpStatus.BAD_REQUEST, request, e); } this.logException(e); this.logError(result); return result; } }
@ExceptionHandler(HttpMessageNotReadableException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public MessageResponse handleHttpMessageNotReadableException(HttpServletRequest request, HttpMessageNotReadableException e) { if (e.getCause() instanceof UnrecognizedPropertyException) { return handleUnrecognizedPropertyException(request, (UnrecognizedPropertyException)e.getCause()); } else { MessageResponse response; if (e.getCause() instanceof JsonProcessingException) { JsonProcessingException jpe = (JsonProcessingException)e.getCause(); response = new MessageResponse(HttpStatus.BAD_REQUEST, buildDate(), request, jpe.getOriginalMessage()); } else { response = new MessageResponse(HttpStatus.BAD_REQUEST, buildDate(), request, e.getMessage()); } log(e, response); return response; } }
/** * Handle the case where a * {@linkplain org.springframework.http.converter.HttpMessageConverter * message converter} cannot read from a HTTP request. * <p> * The default implementation sends an HTTP 400 error, and returns an empty * {@code ModelAndView}. Alternatively, a fallback view could be chosen, or * the HttpMediaTypeNotSupportedException could be rethrown as-is. * * @param ex the HttpMessageNotReadableException to be handled * @param request current HTTP request * @param response current HTTP response */ protected void handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpServletRequest request, HttpServletResponse response) { if (ex.getCause() instanceof JsonParseException) { ResponseUtils.sendFail(request, response, ErrorCodeEnum.JSON_FORMAT_ERROR, ex); } else { ResponseUtils.sendFail(request, response, ErrorCodeEnum.BAD_REQUEST, ex); } }