首页 > 解决方案 > Java 异常和 HTTP 500 错误

问题描述

与服务器端的 Java Exceptions 是否有任何相关性,其500错误代码httpInternal Server Error. 它发生是因为unhandled exceptionsin catchblock 或者可能是因为unchecked引发了异常runtime

对我来说,我怎么能得出结论——它是Apache Camel一个已经spring-boot-starter-parent并且已经在其中Spring Quartz配置的项目。基本上REST通过OAuth 1.0身份验证进行调用。

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

  1. OAuth 1.0认证成功与否
  2. 错误处理程序例程无法捕获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 key的 , consumer key,也是一样的情况。

我认为密钥对于这两种情况都不合适。

不过我很想知道,

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

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

标签: javarestspring-bootoauthswagger

解决方案


您可以在这里做的是添加一个 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 中的泛型,但有一个限制 :))


推荐阅读