Spring @RequestMapping 日志消息?
Spring @RequestMapping log messages?
我试图反序列化来自 Javascript UI 的 JSON 对象,但我一直收到 400 - "The request sent by the client was syntactically incorrect" 错误。请注意,我知道 REST 服务可以正常工作,因为如果我对 JSON 进行硬编码,我可以成功访问该服务,但现在我正尝试从实际的 JS 对象生成 JSON。
Spring 是否生成有助于解决此问题的日志消息?如果是这样,那我该如何设置呢?
您可以将自己的日志记录过滤器添加到处理链中。使用 spring 只需提供以下内容即可启动,例如:
import javax.servlet.Filter;
...
@Configuration / @SpringBootApplication
class Something {
@Bean
public Filter loggingFilter(){
return new AbstractRequestLoggingFilter() {
private final Logger log = LoggerFactory
.getLogger(Something.class);
{
setIncludeClientInfo(true);
setIncludeQueryString(true);
setIncludePayload(true);
}
@Override
protected void beforeRequest(HttpServletRequest request, String message) {
// not needed
}
@Override
protected void afterRequest(HttpServletRequest request, String message) {
log.info(message);
}
};
}
结果类似于
{timestamp and such} : After request [uri=/v1/thing/1007?null;client=127.0.0.1;payload= {
"name": "test7"
]
和
{
"name": "test7"
畸形 body。这不是最好的过滤器,因为它省略了有用的信息,例如 http 方法(POST、GET、..)请求、响应所花费的时间……但是如果你看一下它source 你可以很容易地写出一个更好的满足你需要的。特别是因为 ContentCachingRequestWrapper
和 ContentCachingResponseWrapper
允许您非常容易地获得请求和响应的副本。
除了 servlet 过滤器,您还可以添加 HandlerInterceptor
,例如 https://whosebug.com/a/28022330 - There you have access to SpringMVC specific details but you can't access request & response content without consuming them (compare https://whosebug.com/a/2171633 )
我试图反序列化来自 Javascript UI 的 JSON 对象,但我一直收到 400 - "The request sent by the client was syntactically incorrect" 错误。请注意,我知道 REST 服务可以正常工作,因为如果我对 JSON 进行硬编码,我可以成功访问该服务,但现在我正尝试从实际的 JS 对象生成 JSON。
Spring 是否生成有助于解决此问题的日志消息?如果是这样,那我该如何设置呢?
您可以将自己的日志记录过滤器添加到处理链中。使用 spring 只需提供以下内容即可启动,例如:
import javax.servlet.Filter;
...
@Configuration / @SpringBootApplication
class Something {
@Bean
public Filter loggingFilter(){
return new AbstractRequestLoggingFilter() {
private final Logger log = LoggerFactory
.getLogger(Something.class);
{
setIncludeClientInfo(true);
setIncludeQueryString(true);
setIncludePayload(true);
}
@Override
protected void beforeRequest(HttpServletRequest request, String message) {
// not needed
}
@Override
protected void afterRequest(HttpServletRequest request, String message) {
log.info(message);
}
};
}
结果类似于
{timestamp and such} : After request [uri=/v1/thing/1007?null;client=127.0.0.1;payload= {
"name": "test7"
]
和
{
"name": "test7"
畸形 body。这不是最好的过滤器,因为它省略了有用的信息,例如 http 方法(POST、GET、..)请求、响应所花费的时间……但是如果你看一下它source 你可以很容易地写出一个更好的满足你需要的。特别是因为 ContentCachingRequestWrapper
和 ContentCachingResponseWrapper
允许您非常容易地获得请求和响应的副本。
除了 servlet 过滤器,您还可以添加 HandlerInterceptor
,例如 https://whosebug.com/a/28022330 - There you have access to SpringMVC specific details but you can't access request & response content without consuming them (compare https://whosebug.com/a/2171633 )