Eur*_*nes 37 spring spring-mvc
我使用Jackson/JSON设置了Spring REST,一切正常.
但我故意在消息结构中引入了一个错误,导致了400错误的请求.但是服务器上没有日志输出.我期待的错误将是"杰克逊未知属性异常"或其他什么,但它被捕获并且400错误被发送到客户端,但没有在服务器上的异常日志.
我不想清楚地调试服务器上的所有内容,但我希望像这样的Spring网络级异常明确标记为错误.
打开它的正确方法是什么?
谢谢!
Juk*_*kka 42
@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public void handle(HttpMessageNotReadableException e) {
logger.warn("Returning HTTP 400 Bad Request", e);
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*lch 36
在@ Jukka的回答基础上,您可以使用@ControllerAdvice(在Spring 3.2中引入的)所有控制器全局启用它.它确实需要一些代码,但根据我的经验,您通常最终需要一个全局错误处理配置,这允许您设置断点/轻松检查问题.
下面是一个例子:
@ControllerAdvice
public class ControllerConfig {
@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public void handle(HttpMessageNotReadableException e) {
log.warn("Returning HTTP 400 Bad Request", e);
throw e;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 13
您可以log在 中设置此属性的调试级别application.properties。
logging.level.org.springframework.web= DEBUG
Run Code Online (Sandbox Code Playgroud)
Mik*_*ike 10
万一其他人在这个问题上迷路了,以下内容在Spring Boot 2 / Spring 5中对我有用,不需要更改代码。
我设置日志级别org.springframework.web.servlet.mvc.method.annotation来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'
Run Code Online (Sandbox Code Playgroud)
小智 8
就我而言,问题出在我的@ControllerAdvice-annoted 类中,该类扩展了ResponseEntityExceptionHandler. 当我从一些关于自定义异常处理的随机教程中获得该处理程序代码时,我没有费心去检查该超类正在做什么(我的自定义异常处理工作得很好)。
问题是与其他 Spring MVC 异常一起ResponseEntityExceptionHandler.handleException()处理,将处理逻辑委托给特定的处理方法。MethodArgumentNotValidException由于我没有为任何此类方法(在本例中handleMethodArgumentNotValid())提供返回我自己的响应正文的实现,因此我最终得到了返回带有空正文的响应的默认实现:)。
重写您需要的方法或根本不扩展该类。
检查ResponseEntityExceptionHandler 文档以获取已实现的处理程序方法的列表。
| 归档时间: |
|
| 查看次数: |
24296 次 |
| 最近记录: |