【问题标题】:Java Exception and HTTP 500 ErrorJava 异常和 HTTP 500 错误
【发布时间】:2018-10-31 20:58:21
【问题描述】:

与服务器端的Java Exceptions 和500http 错误代码Internal Server Error 是否有任何相关性。发生这种情况是因为 catch 块中的 unhandled exceptions 或者可能是因为在 runtime 上引发的 unchecked 异常

我怎么能得出结论,对我来说,它是 Apache Camel 项目,它具有 spring-boot-starter-parent 并在内部配置了 Spring Quartz。基本上REST 通过OAuth 1.0 身份验证进行调用。

我正在通过Swagger 测试我的应用程序。我无法得出结论,

  1. OAuth 1.0认证成功与否
  2. Error Handler Routines 无法捕获具体的,- 项目中的error code,因为它基本上是unhandledruntime 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)&#xD;\n   at System.Web.Http.Filters.ActionFilterAttribute.OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)&#xD;\n--- End of stack trace from previous location where exception was thrown ---&#xD;\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()&#xD;\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)&#xD;\n   at System.Web.Http.Filters.ActionFilterAttribute.&lt;CallOnActionExecutedAsync&gt;d__5.MoveNext()&#xD;\n--- End of stack trace from previous location where exception was thrown ---&#xD;\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()&#xD;\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)&#xD;\n   at System.Web.Http.Filters.ActionFilterAttribute.&lt;ExecuteActionFilterAsyncCore&gt;d__0.MoveNext()&#xD;\n--- End of stack trace from previous location where exception was thrown ---&#xD;\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()&#xD;\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)&#xD;\n   at System.Web.Http.Controllers.ActionFilterResult.&lt;ExecuteAsync&gt;d__2.MoveNext()&#xD;\n--- End of stack trace from previous location where exception was thrown ---&#xD;\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()&#xD;\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)&#xD;\n   at System.Web.Http.Filters.AuthorizationFilterAttribute.&lt;ExecuteAuthorizationFilterAsyncCore&gt;d__2.MoveNext()&#xD;\n--- End of stack trace from previous location where exception was thrown ---&#xD;\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()&#xD;\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)&#xD;\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.&lt;SendAsync&gt;d__1.MoveNext()</StackTrace></Error>"

即使是错误的secret keyconsumer key,也是一样的情况。

我认为这两种情况的键都不合适。

不过我想知道,

is 500 错误代码对于未处理的异常和未检查的异常很常见 运行时异常,否则我们将有 400 系列

如果出现 http (500) 错误代码而不是在代码中处理并作为 REST 调用的响应字符串的一部分被响应的自定义 java 错误代码,我如何判断测试场景。

【问题讨论】:

  • 发布处理 400 系列的 try catch 块?
  • 代码被混淆了,所以try..catch处理400系列就是它。万一unchecked exceptions,是400系列还是怎么样?
  • http 500 也会在内存泄漏时遇到,所以你的服务器会在那个时候没有元空间,如果是这种情况,那么它会在每个休息请求上发生

标签: java rest spring-boot oauth swagger


【解决方案1】:

你可以在这里做的是添加一个 ExceptionHandler 并调试 异常的根本原因。看: http://www.baeldung.com/exception-handling-for-rest-with-springhttp://zetcode.com/springboot/exceptionhandler/http://www.springboottutorial.com/spring-boot-exception-handling-for-rest-services

默认情况下,大多数框架和库(用于构建 RESTful API)对于任何未捕获(或运行时)异常都会抛出 500 错误,这仅仅是因为它们无法决定实际的业务逻辑。

但服务器端开发人员有责任处理任何未捕获(或运行时)异常,并根据 REST API 标准或根据业务用例将它们转换为适当的 HTTP 响应代码。请参阅:http://www.restapitutorial.com/httpstatuscodes.html,了解 HTTP 响应代码与用例的理想映射。

附:这就像在问:我们是否应该将一组整数值存储在一个数组中 字符串或整数数组。语言/框架不 要求这些细节,对吧? (我的意思是有时他们会这样做, 比如说,Java 中的泛型,但有一个限制:))

【讨论】:

    猜你喜欢
    • 2021-12-08
    • 1970-01-01
    • 2015-12-05
    • 2016-12-12
    • 2015-04-25
    • 1970-01-01
    • 2013-06-29
    • 2021-06-25
    • 1970-01-01
    相关资源
    最近更新 更多