@Override public Object read(Class<?> clazz, HttpInputMessage inputMessage) { throw new HttpMessageNotReadableException("Could not read", inputMessage); }
@ExceptionHandler(HttpMessageNotReadableException.class) public void messageNotReadableException( HttpMessageNotReadableException exception, HttpServletRequest request, HttpServletResponse response) throws IOException { response.sendError(400, exception.getMessage()); }
@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; } }
@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); }
@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); } }
@ExceptionHandler({HttpMessageNotReadableException.class}) public ResponseEntity<Object> messageNotReadableExceptionHandler(HttpServletRequest req, HttpMessageNotReadableException exception) { logger.error("Message not readable: {}", exception.toString()); return errorResponse(req.getRequestURI(), new StatusCode(CodeEnum.CODE_400)); }
@ExceptionHandler(HttpMessageNotReadableException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public R handleError(HttpMessageNotReadableException e) { log.error("消息不能读取:{}", e.getMessage()); return R.fail(ResultCode.MSG_NOT_READABLE, e.getMessage()); }
@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); } }
@ExceptionHandler({HttpMessageNotReadableException.class}) public ResponseEntity<Object> messageNotReadableExceptionHandler(HttpServletRequest req, HttpMessageNotReadableException exception) { logger.error("Message not readable: {}", exception.toString()); return errorResponse(req.getRequestURI(), new StatusCode(CodeEnum.CODE_400)); }
private Object readResolved(Type resolvedType, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { Reader reader = getReader(inputMessage); try { return readInternal(resolvedType, reader); } catch (Exception ex) { throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex, inputMessage); } }
@ExceptionHandler(HttpMessageNotReadableException.class) @ResponseStatus(value = HttpStatus.BAD_REQUEST) public Object handHttpMessageNotReadableException(HttpMessageNotReadableException e) { return ServerResponse.createByErrorCodeMessage(HttpStatus.BAD_REQUEST.value(), e.getMessage()); }
@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}) public ResponseEntity<Object> messageNotReadableExceptionHandler(HttpServletRequest req, HttpMessageNotReadableException exception) { logger.error("Message not readable: {}", exception.toString()); return errorResponse(req.getRequestURI(), new StatusCode(CodeEnum.CODE_400)); }
@SuppressWarnings("deprecation") // on JDK 9 private SAXSource readSAXSource(InputStream body, HttpInputMessage inputMessage) throws IOException { try { XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd()); xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities()); if (!isProcessExternalEntities()) { xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER); } byte[] bytes = StreamUtils.copyToByteArray(body); return new SAXSource(xmlReader, new InputSource(new ByteArrayInputStream(bytes))); } catch (SAXException ex) { throw new HttpMessageNotReadableException( "Could not parse document: " + ex.getMessage(), ex, inputMessage); } }
@ExceptionHandler(HttpMessageNotReadableException.class) public ResponseEntity<ErrorMessage> handleException(HttpMessageNotReadableException ex) { log.debug("Unprocessable request received: ", ex); return getErrorResponse(ex.getMessage(), HttpStatus.UNPROCESSABLE_ENTITY); }
/** * 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); } }
@ExceptionHandler({HttpMessageNotReadableException.class}) public ResponseEntity<Object> messageNotReadableExceptionHandler(HttpServletRequest req, HttpMessageNotReadableException exception) { logger.error("Message not readable: {}", exception.toString()); return errorResponse(req.getRequestURI(), new StatusCode(CodeEnum.CODE_400)); }
@Override protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { String value = this.stringHttpMessageConverter.readInternal(String.class, inputMessage); Object result = this.conversionService.convert(value, clazz); if (result == null) { throw new HttpMessageNotReadableException( "Unexpected null conversion result for '" + value + "' to " + clazz, inputMessage); } return result; }
/** * 参数解析异常 * * @param e 异常信息 * @return */ @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(HttpMessageNotReadableException.class) public BaseMessage handleHttpMessageNotReadableException(HttpMessageNotReadableException e) { return pubTools.genNoMsg(StatusCode.BAD_REQUEST, e.getMessage()); }
throw new HttpMessageNotReadableException("NPE while unmarshalling: This can happen " + "due to the presence of DTD declarations which are disabled.", ex, inputMessage); throw new HttpMessageNotReadableException( "Could not set feature: " + ex.getMessage(), ex, inputMessage); throw new HttpMessageNotReadableException( "Could not parse document: " + ex.getMessage(), ex, inputMessage);