gpt4 book ai didi

java - RestEasy 中的 405 方法不允许为 Http Status 500

转载 作者:行者123 更新时间:2023-11-30 02:59:15 25 4
gpt4 key购买 nike

目前正在使用 RestEasy 框架开发 Restful 服务。

在处理 405 时遇到麻烦 - javax.ws.rs.NotAllowedException: RESTEASY003650: No resource method found for POST, return 405 with Allow header。

已经使用ExceptionMapperNotAllowedException编写了ErrorHandler,并在扩展javax.ws.rs.core.Application的根应用程序中注册了提供程序.

但它仍然返回 500 错误并将 Http Status 设置为 500 而不是 405。而为 400 和 404 编写的其他处理程序工作正常。

使用RestEasy最新版本:3.0.16.Final

这是 NotAllowedExceptionHandler 的代码并在应用程序中注册相同的代码。

@Provider
public class NotAllowedExceptionHandler implements ExceptionMapper <NotAllowedException>
{
@Override
public Response toResponse( NotAllowedException exception )
{
String bodyOfResponse = exception.getMessage();
APIResponse response = new APIResponse( false , null , Status.METHOD_NOT_ALLOWED , HttpServletResponse.SC_METHOD_NOT_ALLOWED ,
bodyOfResponse );
return Response.status( response.getStatus() ).entity( response ).build();
}
}

public class RootApplication extends Application
{
@Override
public Set <Class <?>> getClasses()
{
Set <Class <?>> classes = new HashSet <Class <?>>();

/* Specify resource classes to be loaded */
classes.add( ErrorResource.class );

/* Specify exception handler classes to be loaded */
classes.add( ResourceGoneExceptionHandler.class );
classes.add( RequestTooLargeExceptionHandler.class );
classes.add( SearchExceptionHandler.class );
classes.add( ServiceUnavailableExceptionHandler.class );
classes.add( UnauthorizedExceptionHandler.class );
classes.add( EntityNotFoundExceptionHandler.class );
classes.add( JDOObjectNotFoundExceptionHandler.class );
classes.add( NucleusObjectNotFoundExceptionHandler.class );
classes.add( ResourceNotFoundExceptionHandler.class );
classes.add( APIExceptionHandler.class );
classes.add( ParseExceptionHandler.class );
classes.add( InternalServerErrorExceptionHandler.class );
classes.add( IllegalArgumentExceptionHandler.class );
classes.add( JDOFatalUserExceptionHandler.class );
classes.add( JDOUserExceptionHandler.class );
classes.add( FatalN`enter code here`ucleusUserExceptionHandler.class );
classes.add( IOExceptionHandler.class );
classes.add( UnrecognizedPropertyExceptionHandler.class );
classes.add( NotAllowedExceptionHandler.class);
classes.add( NotSupportedExceptionHandler.class);

/* Specify filter classes to be loaded */
classes.add( RequestFilter.class );

return classes;

}
}

场景:尝试使用错误的 Http 方法 - POST 而不是 PATCH 执行资源 url。

这是相同的异常堆栈跟踪:

org.jboss.resteasy.spi.UnhandledException: org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of type: com.adaptavant.distributedsource.objects.APIResponse of media type: application/octet-stream
at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:180)
at org.jboss.resteasy.core.SynchronousDispatcher.invokePropagateNotFound(SynchronousDispatcher.java:236)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:225)
at org.jboss.resteasy.plugins.server.servlet.FilterDispatcher.doFilter(FilterDispatcher.java:62)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:37)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.JdbcMySqlConnectionCleanupFilter.doFilter(JdbcMySqlConnectionCleanupFilter.java:60)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:50)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:260)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
at com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:78)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:148)
at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:468)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:437)
at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:444)
at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:256)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:308)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:300)
at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:441)
at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:235)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of type: com.adaptavant.distributedsource.objects.APIResponse of media type: application/octet-stream
at org.jboss.resteasy.core.ServerResponseWriter.writeNomapResponse(ServerResponseWriter.java:66)
at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:176)
... 34 more
E 2016-04-03 14:01:38.444
com.adaptavant.distributedsource.exception.handler.InternalServerErrorExceptionHandler toResponse:
Message: null
Exception Class: class java.lang.Exception
java.lang.Exception
at com.adaptavant.distributedsource.resource.ErrorResource.handlePostError500(ErrorResource.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:44)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:139)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:295)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:249)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:236)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:395)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:202)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:221)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:327)
at org.mortbay.jetty.servlet.Dispatcher.error(Dispatcher.java:135)
at org.mortbay.jetty.Response.sendError(Response.java:274)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:475)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:437)
at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:444)
at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:256)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:308)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:300)
at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:441)
at java.lang.Thread.run(Thread.java:745)

请在这方面提供帮助。

提前致谢。

最佳答案

RestEasy 不知道如何处理 APIResponse。在 NotAllowedExceptionHandler.toResponse 方法的 Response 中添加类型以解决问题:

Response.status( response.getStatus() ).entity( response ).type(MediaType.<type>).build(); 

对于例如 JSON 类型,代码如下所示:

Response.status( response.getStatus() ).entity( response ).type(MediaType.APPLICATION_JSON).build(); 

完整方法:

 @Provider
public class NotAllowedExceptionHandler implements ExceptionMapper <NotAllowedException>
{
@Override
public Response toResponse( NotAllowedException exception )
{
String bodyOfResponse = exception.getMessage();
APIResponse response = new APIResponse( false , null , Status.METHOD_NOT_ALLOWED , HttpServletResponse.SC_METHOD_NOT_ALLOWED ,
bodyOfResponse );
return Response.status( response.getStatus() ).entity( response ).type(MediaType.APPLICATION_JSON).build();
}
}

关于java - RestEasy 中的 405 方法不允许为 Http Status 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36374177/

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