Spring ResponseEntityExceptionHandler和原始POST有效载荷 [英] Spring ResponseEntityExceptionHandler and original POST payload

查看:378
本文介绍了Spring ResponseEntityExceptionHandler和原始POST有效载荷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在基于 Spring Boot 的应用程序中使用 ResponseEntityExceptionHandler 来捕获单个错误并输出一致的错误有效内容。

I'm using ResponseEntityExceptionHandler in a Spring Boot-based application, to capture errors in a single place and output a consistent error payload.

一切都符合预期。
ResponseEntityExceptionHandler 有一个方法 handleHttpMessageNotReadable ,可以用于在发送无效消息的客户端上反应 (在我的情况下是一个JSON有效载荷)。

Everything works as expected. ResponseEntityExceptionHandler has a method handleHttpMessageNotReadable that can be used to "react" on a client sending an invalid message (in my case a JSON payload).

同样,一切都按预期工作。如果客户端 POST 一个无效的JSON文档, handleHttpMessageNotReadable 中的代码正确执行。

Again, everything works as expected. If a client POST an invalid JSON document, the code in the handleHttpMessageNotReadable is executed correctly.

现在,为了监控的目的,我想记录无效的JSON。
这是我在 handleHttpMessageNotReadable 方法中使用的代码,以获取有效载荷。

Now, for monitoring purposes, I would like to log the invalid JSON. This is the code I'm using inside the handleHttpMessageNotReadable method, to get hold of the payload.

String line = null;
StringBuffer jb = new StringBuffer();
try {
    BufferedReader reader = servletWebRequest.getRequest().getReader();
    while ((line = reader.readLine()) != null)
       jb.append(line);

} catch (IOException e) {
        e.printStackTrace();
}

正如预期的那样,我收到一个例外,因为流已经被杰克逊,用于实际解析JSON有效负载:

As expected, I get an exception because the stream has been already consumed by Jackson, for actually parsing the JSON payload:

java.lang.IllegalStateException: getInputStream() has already been called for this request

访问原始POST有效内容的简单方法是什么?
可以抛出一个包含原始有效负载的定制异常( MyException extends HttpMessageNotReadableException )?

What would be a simple approach to access the original POST payload? Would it be possible to throw a custom exception which would contain the original payload (MyException extends HttpMessageNotReadableException)?

推荐答案

请求体从JSON转换为java类型
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter

The request body is converted from JSON into a java type by org.springframework.http.converter.json.MappingJackson2HttpMessageConverter

如果发生错误,HttpMessageNotReadableException抛出在
超类org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter#readJavaType

If erros occur, the HttpMessageNotReadableException is thrown in the super class org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter#readJavaType

你可以子类MappingJackson2HttpMessageConverter,覆盖readJavaType(..)并尝试抛出自己的异常或定制的HttpMessageNotReadableException。

You can subclass MappingJackson2HttpMessageConverter, overwrite readJavaType(..) and try to throw your own exception or a customized HttpMessageNotReadableException there.

很可能你需要克隆输入流,使用一个来尝试反序列化JSON,另一个用于在需要抛出的情况下使用一个例外。
这可能会影响性能,但如果没有问题,可以尝试一下。

Most likely you will need to clone the input stream, use one to try to deserialize the JSON, and another to use in case you need to throw an exception. This may impact performance, but if thats not a problem, you may give it a try....

这篇关于Spring ResponseEntityExceptionHandler和原始POST有效载荷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆