org.springframework.http.converter.HttpMessageNotReadableException Java Examples

The following examples show how to use org.springframework.http.converter.HttpMessageNotReadableException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SourceHttpMessageConverter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	InputStream body = inputMessage.getBody();
	if (DOMSource.class == clazz) {
		return (T) readDOMSource(body);
	}
	else if (SAXSource.class == clazz) {
		return (T) readSAXSource(body);
	}
	else if (StAXSource.class == clazz) {
		return (T) readStAXSource(body);
	}
	else if (StreamSource.class == clazz || Source.class == clazz) {
		return (T) readStreamSource(body);
	}
	else {
		throw new HttpMessageConversionException("Could not read class [" + clazz +
				"]. Only DOMSource, SAXSource, StAXSource, and StreamSource are supported.");
	}
}
 
Example #2
Source File: AbstractWireFeedHttpMessageConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	WireFeedInput feedInput = new WireFeedInput();
	MediaType contentType = inputMessage.getHeaders().getContentType();
	Charset charset = (contentType != null && contentType.getCharset() != null ?
			contentType.getCharset() : DEFAULT_CHARSET);
	try {
		Reader reader = new InputStreamReader(inputMessage.getBody(), charset);
		return (T) feedInput.build(reader);
	}
	catch (FeedException ex) {
		throw new HttpMessageNotReadableException("Could not read WireFeed: " + ex.getMessage(), ex);
	}
}
 
Example #3
Source File: SourceHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void readDomSourceWithXmlBomb() throws Exception {
	// https://en.wikipedia.org/wiki/Billion_laughs
	// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
	String content = "<?xml version=\"1.0\"?>\n" +
			"<!DOCTYPE lolz [\n" +
			" <!ENTITY lol \"lol\">\n" +
			" <!ELEMENT lolz (#PCDATA)>\n" +
			" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
			" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
			" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
			" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
			" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
			" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
			" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
			" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
			" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
			"]>\n" +
			"<root>&lol9;</root>";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));

	this.thrown.expect(HttpMessageNotReadableException.class);
	this.thrown.expectMessage("DOCTYPE");

	this.converter.read(DOMSource.class, inputMessage);
}
 
Example #4
Source File: Fastjson2HttpMessageConverter.java    From dpCms with Apache License 2.0 6 votes vote down vote up
@Override
protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException,  HttpMessageNotReadableException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    InputStream in = inputMessage.getBody();
    byte[] buf = new byte[1024];
    for (;;) {
        int len = in.read(buf);
        if (len == -1) {
            break;
        }
        if (len > 0) {
            baos.write(buf, 0, len);
        }
    }
    byte[] bytes = baos.toByteArray();
    return JSON.parseObject(bytes, 0, bytes.length, charset.newDecoder(), clazz);
}
 
Example #5
Source File: Jaxb2RootElementHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testXmlBomb() throws Exception {
	// https://en.wikipedia.org/wiki/Billion_laughs
	// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
	String content = "<?xml version=\"1.0\"?>\n" +
			"<!DOCTYPE lolz [\n" +
			" <!ENTITY lol \"lol\">\n" +
			" <!ELEMENT lolz (#PCDATA)>\n" +
			" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
			" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
			" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
			" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
			" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
			" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
			" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
			" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
			" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
			"]>\n" +
			"<rootElement><external>&lol9;</external></rootElement>";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
	assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() ->
			this.converter.read(RootElement.class, inputMessage))
		.withMessageContaining("DOCTYPE");
}
 
Example #6
Source File: SourceHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void readDomSourceWithXmlBomb() throws Exception {
	// https://en.wikipedia.org/wiki/Billion_laughs
	// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
	String content = "<?xml version=\"1.0\"?>\n" +
			"<!DOCTYPE lolz [\n" +
			" <!ENTITY lol \"lol\">\n" +
			" <!ELEMENT lolz (#PCDATA)>\n" +
			" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
			" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
			" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
			" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
			" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
			" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
			" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
			" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
			" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
			"]>\n" +
			"<root>&lol9;</root>";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));

	assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() ->
			this.converter.read(DOMSource.class, inputMessage))
		.withMessageContaining("DOCTYPE");
}
 
Example #7
Source File: MappingJackson2XmlHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void readWithXmlBomb() throws IOException {
	// https://en.wikipedia.org/wiki/Billion_laughs
	// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
	String body = "<?xml version=\"1.0\"?>\n" +
			"<!DOCTYPE lolz [\n" +
			" <!ENTITY lol \"lol\">\n" +
			" <!ELEMENT lolz (#PCDATA)>\n" +
			" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
			" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
			" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
			" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
			" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
			" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
			" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
			" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
			" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
			"]>\n" +
			"<MyBean>&lol9;</MyBean>";

	MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
	inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));

	assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() ->
			this.converter.read(MyBean.class, inputMessage));
}
 
Example #8
Source File: SourceHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void readDomSourceWithXmlBomb() throws Exception {
	// https://en.wikipedia.org/wiki/Billion_laughs
	// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
	String content = "<?xml version=\"1.0\"?>\n" +
			"<!DOCTYPE lolz [\n" +
			" <!ENTITY lol \"lol\">\n" +
			" <!ELEMENT lolz (#PCDATA)>\n" +
			" <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" +
			" <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" +
			" <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" +
			" <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" +
			" <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" +
			" <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" +
			" <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" +
			" <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" +
			" <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" +
			"]>\n" +
			"<root>&lol9;</root>";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));

	this.thrown.expect(HttpMessageNotReadableException.class);
	this.thrown.expectMessage("DOCTYPE");

	this.converter.read(DOMSource.class, inputMessage);
}
 
Example #9
Source File: SourceHttpMessageConverter.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	InputStream body = inputMessage.getBody();
	if (DOMSource.class == clazz) {
		return (T) readDOMSource(body, inputMessage);
	}
	else if (SAXSource.class == clazz) {
		return (T) readSAXSource(body, inputMessage);
	}
	else if (StAXSource.class == clazz) {
		return (T) readStAXSource(body, inputMessage);
	}
	else if (StreamSource.class == clazz || Source.class == clazz) {
		return (T) readStreamSource(body);
	}
	else {
		throw new HttpMessageNotReadableException("Could not read class [" + clazz +
				"]. Only DOMSource, SAXSource, StAXSource, and StreamSource are supported.", inputMessage);
	}
}
 
Example #10
Source File: JsonApiHttpMessageConverter.java    From JuniperBot with GNU General Public License v3.0 6 votes vote down vote up
@Override
public JSONAPIDocument read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    if (!isAcceptable(type)) {
        throw new IllegalArgumentException("Type not supported");
    }
    ParameterizedType rootType = getSubType(type);
    if (rootType != null && Collection.class.isAssignableFrom((Class) rootType.getRawType())) {
        Class<?> collectionType = getSubClass(rootType);
        if (collectionType != null) {
            return converter.readDocumentCollection(inputMessage.getBody(), collectionType);
        }
    }
    Class<?> rootClass = getSubClass(type);
    if (rootClass != null) {
        return converter.readDocument(inputMessage.getBody(), rootClass);
    }
    throw new IllegalArgumentException("No collection subtype found");
}
 
Example #11
Source File: MarshallingHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
	}
}
 
Example #12
Source File: AbstractJackson2HttpMessageConverter.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
	try {
		if (inputMessage instanceof MappingJacksonInputMessage) {
			Class<?> deserializationView = ((MappingJacksonInputMessage) inputMessage).getDeserializationView();
			if (deserializationView != null) {
				return this.objectMapper.readerWithView(deserializationView).withType(javaType).
						readValue(inputMessage.getBody());
			}
		}
		return this.objectMapper.readValue(inputMessage.getBody(), javaType);
	}
	catch (IOException ex) {
		throw new HttpMessageNotReadableException("Could not read document: " + ex.getMessage(), ex);
	}
}
 
Example #13
Source File: WxMediaResourceMessageConverter.java    From FastBootWeixin with Apache License 2.0 6 votes vote down vote up
@Override
    protected Resource readInternal(Class<? extends Resource> clazz, HttpInputMessage inputMessage)
            throws IOException, HttpMessageNotReadableException {

        WxMediaResource wxMediaResource = new WxMediaResource(inputMessage);
        if (wxMediaResource.isUrlMedia() && !clazz.isAssignableFrom(WxMediaResource.class)) {
            throw new WxApiException("不支持的返回类型,接口返回了url");
        }
        if (InputStreamResource.class == clazz) {
            return new InputStreamResource(wxMediaResource.getInputStream());
        } else if (clazz.isAssignableFrom(WxMediaResource.class)) {
            return wxMediaResource;
        } else if (clazz.isAssignableFrom(ByteArrayResource.class)) {
            return new ByteArrayResource(wxMediaResource.getBody());
        } else if (clazz.isAssignableFrom(FileSystemResource.class)) {
            return new FileSystemResource(wxMediaResource.getFile());
        }
//		else if (clazz.isAssignableFrom(File.class)) {
//			return wxMediaResource.getFile();
//		}
        throw new WxApiException("不支持的返回类型");
    }
 
Example #14
Source File: RequestResponseBodyMethodProcessor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter methodParam,
		Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {

	HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
	ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(servletRequest);

	Object arg = readWithMessageConverters(inputMessage, methodParam, paramType);
	if (arg == null) {
		if (methodParam.getParameterAnnotation(RequestBody.class).required()) {
			throw new HttpMessageNotReadableException("Required request body is missing: " +
					methodParam.getMethod().toGenericString());
		}
	}
	return arg;
}
 
Example #15
Source File: FastjsonHttpMessageConverter.java    From java-platform with Apache License 2.0 5 votes vote down vote up
@Override
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {
	String json = IOUtils.toString(inputMessage.getBody(), getCharset(inputMessage.getHeaders()));
	DefaultJSONParser parser = new DefaultJSONParser(json, this.parserConfig);
	Object value = parser.parseObject(type);

	parser.handleResovleTask(value);

	parser.close();

	return value;
}
 
Example #16
Source File: BaseController.java    From ankush with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Exception handler.
 *
 * @param e the e
 * @return the response entity
 */
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseBody
public ResponseEntity<String> exceptionHandler(
		HttpMessageNotReadableException e) {
	log.error("Invalid request body", e);
	return new ResponseEntity<String>(e.getMessage(),
			HttpStatus.BAD_REQUEST);
}
 
Example #17
Source File: GlobalExceptionHandler.java    From Spring-Boot-Book with Apache License 2.0 5 votes vote down vote up
/**
 * 400 - Bad Request
 */
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(HttpMessageNotReadableException.class)
public String handleHttpMessageNotReadableException(HttpMessageNotReadableException e, Model model) {
    logger.error("参数解析失败", e);
    String message = "【参数解析失败】" + e.getMessage();
    model.addAttribute("message", message);
    model.addAttribute("code", 400);
    return viewName;
}
 
Example #18
Source File: ProtobufHttpMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected Message readInternal(Class<? extends Message> clazz, HttpInputMessage inputMessage)
		throws IOException, HttpMessageNotReadableException {

	MediaType contentType = inputMessage.getHeaders().getContentType();
	if (contentType == null) {
		contentType = PROTOBUF;
	}
	Charset charset = contentType.getCharset();
	if (charset == null) {
		charset = DEFAULT_CHARSET;
	}

	Message.Builder builder = getMessageBuilder(clazz);
	if (PROTOBUF.isCompatibleWith(contentType)) {
		builder.mergeFrom(inputMessage.getBody(), this.extensionRegistry);
	}
	else if (TEXT_PLAIN.isCompatibleWith(contentType)) {
		InputStreamReader reader = new InputStreamReader(inputMessage.getBody(), charset);
		TextFormat.merge(reader, this.extensionRegistry, builder);
	}
	else if (this.protobufFormatSupport != null) {
		this.protobufFormatSupport.merge(
				inputMessage.getBody(), charset, contentType, this.extensionRegistry, builder);
	}
	return builder.build();
}
 
Example #19
Source File: SOAPTest.java    From riptide with MIT License 5 votes vote down vote up
@Test
void shouldFailToRead() {
    final CompletableFuture<ClientHttpResponse> future = unit.post()
            .body(new SayHello("Riptide"))
            .call(soap(InvalidSayHelloResponse.class, System.out::println));

    final CompletionException exception = assertThrows(CompletionException.class, future::join);
    assertThat(exception.getCause(), is(instanceOf(HttpMessageNotReadableException.class)));
}
 
Example #20
Source File: BaseAppController.java    From Juice with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 用于处理HttpMessageNotReadableException
 *
 * @param e
 * @return
 */
@ResponseBody
@ExceptionHandler({ HttpMessageNotReadableException.class })
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public Result exception(HttpMessageNotReadableException e) throws IOException {
    //log.warn("got a Exception",e);

    Integer status = REQUEST_PARAMS_INVALID_ERROR.getStatus();
    String message = REQUEST_PARAMS_INVALID_ERROR.getMessage();

    return handleValue(status, message);
}
 
Example #21
Source File: SerializableObjectHttpMessageConverter.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
@Override
protected Serializable readInternal(final Class<? extends Serializable> type, final HttpInputMessage inputMessage)
  throws IOException, HttpMessageNotReadableException
{
  try {
    return type.cast(IOUtils.deserializeObject(IOUtils.toByteArray(inputMessage.getBody()),
      ObjectUtils.defaultIfNull(type.getClassLoader(), getClass().getClassLoader())));
  }
  catch (ClassNotFoundException e) {
    throw new HttpMessageNotReadableException(String.format(
      "Unable to convert the HTTP message body into an Object of type (%1$s)", type.getName()), e);
  }
}
 
Example #22
Source File: RestExceptionHandler.java    From mogu_blog_v2 with Apache License 2.0 5 votes vote down vote up
/**
 * 400错误
 *
 * @param ex
 * @return
 */
@ExceptionHandler({HttpMessageNotReadableException.class})
@ResponseBody
public Result requestNotReadable(HttpMessageNotReadableException ex) {
    log.error("异常类 HttpMessageNotReadableException {},", ex.getMessage());
    return Result.createWithErrorMessage("参数异常", ErrorConstants.PARAM_INCORRECT);
}
 
Example #23
Source File: HttpConverterJson.java    From mercury with Apache License 2.0 5 votes vote down vote up
@Override
public Object read(Class<?> clazz, HttpInputMessage inputMessage) throws HttpMessageNotReadableException {
    if (inputMessage != null) {
        try {
            // validate class with white list before loading the input stream
            SimpleObjectMapper mapper = SimpleMapper.getInstance().getWhiteListMapper(clazz);
            String input = util.stream2str(inputMessage.getBody());
            return mapper.readValue(input, clazz);
        } catch (IOException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    } else {
        return null;
    }
}
 
Example #24
Source File: ExceptionProcessor.java    From personal_book_library_web_project with MIT License 5 votes vote down vote up
@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ServiceExceptionDetails processHttpMessageNotReadableException(HttpMessageNotReadableException exception) {
	
	String detailedErrorMessage = exception.getMessage();

    return new ServiceExceptionDetails("", detailedErrorMessage, "");
}
 
Example #25
Source File: GlobalExceptionTranslator.java    From staffjoy with MIT License 5 votes vote down vote up
@ExceptionHandler(HttpMessageNotReadableException.class)
public BaseResponse handleError(HttpMessageNotReadableException e) {
    logger.error("Message Not Readable", e);
    return BaseResponse
            .builder()
            .code(ResultCode.MSG_NOT_READABLE)
            .message(e.getMessage())
            .build();
}
 
Example #26
Source File: MappingJackson2XmlHttpMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void readWithExternalReference() throws IOException {
	String body = "<!DOCTYPE MyBean SYSTEM \"https://192.168.28.42/1.jsp\" [" +
			"  <!ELEMENT root ANY >\n" +
			"  <!ENTITY ext SYSTEM \"" +
			new ClassPathResource("external.txt", getClass()).getURI() +
			"\" >]><MyBean><string>&ext;</string></MyBean>";

	MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
	inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));

	assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() ->
			this.converter.read(MyBean.class, inputMessage));
}
 
Example #27
Source File: MappingJackson2XmlHttpMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void readInvalidXml() throws IOException {
	String body = "FooBar";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
	inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
	assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() ->
			converter.read(MyBean.class, inputMessage));
}
 
Example #28
Source File: GlobalExceptionHandler.java    From SENS with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 400 - Bad Request
 */
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(HttpMessageNotReadableException.class)
public String handleHttpMessageNotReadableException(HttpMessageNotReadableException e, Model model) {
    log.error("参数解析失败", e);
    String message = "【参数解析失败】" + e.getMessage();
    model.addAttribute("message", message);
    model.addAttribute("code", 400);
    return viewName;
}
 
Example #29
Source File: YahooIntraDayHistoMessageConverter.java    From cloudstreetmarket.com with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected QuoteWrapper readInternal(Class<? extends QuoteWrapper> clazz, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException {
    CSVReader reader = new CSVReader(new InputStreamReader(httpInputMessage.getBody()));
    List<String[]> rows = reader.readAll();
    QuoteWrapper quoteWrapper = new QuoteWrapper();
    for (String[] row : rows) {
    	
    	try{
    		Integer.valueOf(row[0]);
    	}
    	catch(NumberFormatException e){
    		break;
    	}

    	quoteWrapper.add(new YahooQuote(row[0], 
    								row[1], 
    								parseDouble(row[2]), 
    								parseDouble(row[3]), 
    								parseDouble(row[4]), 
    								parseDouble(row[5]), 
    								parsePercent(row[6]), 
    								parseDouble(row[7]), 
    								parseDouble(row[8]), 
    								parseDouble(row[9]), 
    								parseDouble(row[10]), 
    								parseInt(row[11]), 
    								row[12], 
    								row[13]));
    }

    return quoteWrapper;
}
 
Example #30
Source File: RequestBodyServiceParameterResolver.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Override
public Object resolve(RestMethodMetadata restMethodMetadata,
		MethodParameterMetadata methodParameterMetadata, HttpServerRequest request) {

	if (!supportParameter(restMethodMetadata, methodParameterMetadata)) {
		return null;
	}

	Object result = null;

	Class<?> parameterType = resolveClass(methodParameterMetadata.getType());

	HttpMessageConverterHolder holder = httpMessageConverterResolver.resolve(request,
			parameterType);

	if (holder != null) {
		HttpMessageConverter converter = holder.getConverter();
		try {
			result = converter.read(parameterType, request);
		}
		catch (IOException e) {
			throw new HttpMessageNotReadableException(
					"I/O error while reading input message", e);
		}
	}

	return result;
}