405 Resting中的Http Status 500

时间:2016-04-02 13:57:24

标签: java resteasy

目前正在使用RestEasy Framework开发Restful Service。

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

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

但它仍然返回500错误并将Http状态设置为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。

以下是相同的异常堆栈跟踪:

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)

请在这方面提供帮助。

提前致谢。

1 个答案:

答案 0 :(得分:2)

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

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();
                }
        }