Spring REST 使用 Jackson - 400 错误请求日志记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17326976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:02:44  来源:igfitidea点击:

Spring REST using Hymanson - 400 bad request logging

springspring-mvc

提问by Eurig Jones

I have spring REST set up fine using Hymanson/JSON and everything works.

我使用 Hymanson/JSON 设置了 Spring REST,一切正常。

But I knowingly introduced an error in the structure of the message which resulted in a 400 - Bad Request. But there was no log output on the server. The error I would be expecting would be something like "Hymanson unknown property exception" or whatever but it was caught and a 400 error was sent to the client, but no log of the exception on the server.

但是我故意在消息结构中引入了一个错误,导致 400 - Bad Request。但是服务器上没有日志输出。我期望的错误类似于“Hymanson 未知属性异常”之类的东西,但它被捕获并且 400 错误被发送到客户端,但服务器上没有异常日志。

I don't want to debug everything on the server clearly, but I want Spring network level exceptions like this clearly labelled as error.

我不想清楚地调试服务器上的所有内容,但我希望像这样的 Spring 网络级异常清楚地标记为错误。

What is the correct way to switch this on?

开启此功能的正确方法是什么?

Thanks!

谢谢!

回答by Jukka

@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public void handle(HttpMessageNotReadableException e) {
    logger.warn("Returning HTTP 400 Bad Request", e);
}

回答by David Welch

Building on @Jukka's answer, you can enable this globally for all controllers using @ControllerAdvice(introduced in Spring 3.2). It does require a little code on your end, but in my experience you usually end up needing a global error handling configuration anyways and this allows you to set breakpoints / easily inspect the problems.

基于@Jukka 的回答,您可以为所有使用的控制器全局启用此功能@ControllerAdvice(在 Spring 3.2 中引入)。它确实需要一些代码,但根据我的经验,您通常最终需要全局错误处理配置,这使您可以设置断点/轻松检查问题。

An example of this is below:

下面是一个例子:

@ControllerAdvice
public class ControllerConfig {

    @ExceptionHandler
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public void handle(HttpMessageNotReadableException e) {
        log.warn("Returning HTTP 400 Bad Request", e);
        throw e;
    }
}

回答by Mike

In case anyone else stumbles on this issue, the following worked for me in Spring Boot 2 / Spring 5 and didn't require a code change.

万一其他人偶然发现这个问题,以下内容在 Spring Boot 2 / Spring 5 中对我有用,不需要更改代码。

I set the log level for org.springframework.web.servlet.mvc.method.annotationto DEBUG. In my case I was seeing 400 responses in my client code but no logging on the server side (even with a custom error handler). After changing that log level the problem was obvious:

我将日志级别设置为org.springframework.web.servlet.mvc.method.annotationto DEBUG。就我而言,我在客户端代码中看到 400 个响应,但没有在服务器端进行日志记录(即使使用自定义错误处理程序)。更改日志级别后,问题很明显:

DEBUG 172.19.0.16     cs              2018-Jun-08 20:55:08.415 [https-jsse-nio-443-exec-9] - method.annotation.RequestResponseBodyMethodProcessor[line ?] - Read [class java.lang.String] as "application/xml;charset=UTF-8" with [org.springframework.http.converter.xml.MarshallingHttpMessageConverter@2de50ee4]
DEBUG 172.19.0.16     cs              2018-Jun-08 20:55:08.418 [https-jsse-nio-443-exec-9] - method.annotation.ServletInvocableHandlerMethod[line ?] - Failed to resolve argument 0 of type 'java.lang.String'
org.springframework.beans.TypeMismatchException: Failed to convert value of type 'core.dto.RequestDTO' to required type 'java.lang.String'

回答by L_Arm

In my case, the problem was in my @ControllerAdvice-annoted class, which extended ResponseEntityExceptionHandler. As I got that handler code from some random tutorial about custom exception handling, I didn't bother to check what that superclass was doing (my custom exception handling worked just fine).

就我而言,问题出在我的@ControllerAdvice-annoted 类中,它扩展了ResponseEntityExceptionHandler. 当我从一些关于自定义异常处理的随机教程中获得处理程序代码时,我没有费心去检查那个超类在做什么(我的自定义异常处理工作得很好)。

The problem is ResponseEntityExceptionHandler.handleException()handles MethodArgumentNotValidExceptionalong with other Spring MVC exceptions, delegating handling logic to specific handle- methods. As I did not provide an implementation for any of such methods (in this case handleMethodArgumentNotValid()) to return my own response body, I ended up with the default implementation that returns a response with null body :).

问题是ResponseEntityExceptionHandler.handleException()句柄MethodArgumentNotValidException与其他 Spring MVC 异常一起,将处理逻辑委托给特定的句柄方法。由于我没有为任何此类方法(在本例中handleMethodArgumentNotValid())提供实现以返回我自己的响应正文,因此我最终得到了返回带有 null 正文的响应的默认实现:)。

Solution

解决方案

Override the methods you need or don't extend that class at all.

覆盖您需要的方法或根本不扩展该类。

Check ResponseEntityExceptionHandler docsfor the list of hanler- methods implemented.

检查ResponseEntityExceptionHandler 文档以获取已实现的处理程序方法列表。

回答by Ashok Prajapati

I don't think that it is mandatory to extend ResponseEntityExceptionHandler. You can create multiple handler methods in ControllerAdvice class annotated with ExceptionHandler and you can return any kind of response. Please check below link for more details and code example. https://www.thetechnojournals.com/2019/11/how-to-handle-exceptions-in-rest.html

我认为扩展 ResponseEntityExceptionHandler 不是强制性的。您可以在使用 ExceptionHandler 注释的 ControllerAdvice 类中创建多个处理程序方法,并且您可以返回任何类型的响应。请查看以下链接以获取更多详细信息和代码示例。 https://www.thetechnojournals.com/2019/11/how-to-handle-exceptions-in-rest.html