Java异常和HTTP 500错误

时间:2018-05-22 07:11:11

标签: java rest spring-boot oauth swagger

与服务器端的Java异常和500错误代码http是否Internal Server Error有任何关联。这是因为unhandled exceptions块中的catch,或者可能是因为unchecked

引发的runtime例外

我怎么能得出结论,对我而言,Apache Camel项目有spring-boot-starter-parentSpring Quartz已配置。基本上REST会通过OAuth 1.0身份验证检查来调用。

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

  1. OAuth 1.0身份验证是否成功
  2. 错误处理程序例程无法捕获项目中的特定字段error code,因为unhandledruntime exception基本上
  3. 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,情况也是如此。

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

    但我想知道,

      

    是500错误代码,对于未处理的异常是常见的未选中   运行时异常或那里我们将是400系列

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

1 个答案:

答案 0 :(得分:1)

  

这里你可以做的是添加一个ExceptionHandler并调试   异常的根本原因。看到:   https://gitlab-01example.com或   http://www.baeldung.com/exception-handling-for-rest-with-springhttp://zetcode.com/springboot/exceptionhandler/

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

但服务器端开发人员有责任处理任何未捕获(或运行时)异常,并根据REST API标准或根据业务用例将其转换为正确的HTTP响应代码。有关用例的HTTP响应代码的理想映射,请参阅:http://www.springboottutorial.com/spring-boot-exception-handling-for-rest-services

  

P.S。这就像问:我们应该在数组中存储一组整数值   字符串或整数数组。语言/框架没有   强制执行这些细节,对吧? (我的意思是有时他们这样做,   比方说,Java中的泛型,但有一个限制:))