java - Java 异常和 HTTP 500 错误
问题描述
与服务器端的 Java Exceptions 是否有任何相关性,其500
错误代码http
是Internal Server Error
. 它发生是因为unhandled exceptions
in catch
block 或者可能是因为unchecked
引发了异常runtime
对我来说,我怎么能得出结论——它是Apache Camel
一个已经spring-boot-starter-parent
并且已经在其中Spring Quartz
配置的项目。基本上REST
通过OAuth 1.0
身份验证进行调用。
我正在测试我的应用程序Swagger
。我无法得出结论,
OAuth 1.0
认证成功与否- 错误处理程序例程无法捕获
error code
项目中的具体情况unhandled
,runtime exception
基本上是这样
Swagger 给出以下响应,并带有 500 错误代码。
"<Error><Message>An error has occurred.</Message><ExceptionMessage>Object reference not set to an instance of an object.</ExceptionMessage><ExceptionType>System.NullReferenceException</ExceptionType><StackTrace> at API.ExecutionTimeFilterAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
\n at System.Web.Http.Filters.ActionFilterAttribute.OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
\n--- End of stack trace from previous location where exception was thrown ---
\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
\n at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()
\n--- End of stack trace from previous location where exception was thrown ---
\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
\n at System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()
\n--- End of stack trace from previous location where exception was thrown ---
\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
\n--- End of stack trace from previous location where exception was thrown ---
\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
\n at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()
\n--- End of stack trace from previous location where exception was thrown ---
\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()</StackTrace></Error>"
即使是错误secret key
的 , consumer key
,也是一样的情况。
我认为密钥对于这两种情况都不合适。
不过我很想知道,
500 错误代码对于未处理的异常和未检查的运行时异常很常见,否则我们将有 400 系列
如果出现 http (500) 错误代码而不是在代码中处理并作为REST
调用响应字符串的一部分被响应的自定义 java 错误代码,我如何判断测试场景。
解决方案
您可以在这里做的是添加一个 ExceptionHandler 并调试异常的根本原因。请参阅: http ://www.baeldung.com/exception-handling-for-rest-with-spring或 http://zetcode.com/springboot/exceptionhandler/或http://www.springboottutorial.com/spring-boot -休息服务的异常处理
大多数框架和库(用于构建 RESTful API)默认情况下会为任何未捕获(或运行时)异常抛出 500 错误,这仅仅是因为它们无法决定实际的业务逻辑。
但是服务器端开发人员有责任处理任何未捕获的(或运行时)异常,并根据 REST API 标准或根据业务用例将它们转换为适当的 HTTP 响应代码。请参阅:http ://www.restapitutorial.com/httpstatuscodes.html以获取针对用例的 HTTP 响应代码的理想映射。
PS这就像在问:我们应该将一组整数值存储在字符串数组还是整数数组中。语言/框架不要求这些细节,对吧?(我的意思是有时他们会这样做,比如 Java 中的泛型,但有一个限制 :))
推荐阅读
- tensorflow - 为什么我的 IoU 在使用 tensorflow / keras 的训练中不断减少?
- jenkins - 即使响应断言失败,詹金斯构建也会成功
- node.js - Gulp 在 VS2017 中退出了女巫代码 1 - 构建失败
- c++ - 如何安全地将 ssize_t 与 int64_t 进行比较
- android - Android Safe Args, nav_graph and enums error: 不兼容的枚举比较总是不成功
- java - AEM 6.5 Venia - 控制台中的 Corevenia.core 抛出错误
- android - cz.msebera.android.httpclient.conn.BasicManagedEntity 无法转换为 java.io.InputStream
- javascript - 如何在nestjs中实现icalEvent以通过电子邮件发送日历邀请
- swift - 获取联系人:无法转换“CNLabeledValue”类型的值
?到预期的参数类型“字符串” - module - 英特尔的 oneAPI Fortran 编译器使用 DFWIN 或 IFWIN 模块有什么区别?