gpt4 book ai didi

Java 异常和 HTTP 500 错误

转载 作者:行者123 更新时间:2023-12-02 11:13:17 24 4
gpt4 key购买 nike

与服务器端的 Java 异常和 500 有任何相关性吗?错误代码 http这是 Internal Server Error 。发生这种情况是因为 unhandled exceptionscatch阻止或者可能是因为 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 ,都是同样的场景。

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

但是我想知道,

is 500 error code is common for unhandled exceptions & unchecked runtime exceptions or there we will be 400 series

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

最佳答案

What you could do here is to add an ExceptionHandler and debug the root cause of the exception. See: http://www.baeldung.com/exception-handling-for-rest-with-spring OR http://zetcode.com/springboot/exceptionhandler/ OR http://www.springboottutorial.com/spring-boot-exception-handling-for-rest-services

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

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

P.S. It is like asking: should we store a set of integer values in an array of Strings or in an array of integers. Language/Framework does not mandate these nitty gritty details, right? (I mean sometimes they do, with say, Generics in Java, but there is a limit :))

关于Java 异常和 HTTP 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50461783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com