0

我正在使用 JBoss Resteasy 2.3.1 开发 JAX-RS Web 服务,并尝试遵循 [set-response-headers-not-using-filter-resteasy][1] 中提供的解决方案,但就我而言,这没用。

服务器代码:

@WebService(targetNamespace = "http://com.server/loginws", name = "LoginWS")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL)
@Path("/rpcLoginWS")
public class LoginWSImplRest {
@Context 
    protected org.jboss.resteasy.spi.HttpResponse response;

    @WebMethod(operationName = "login", action = "urn:login")
    @WebResult(name = "return")
    @GET
    @Path("/login/{userName}/{password}")
    @Produces(MediaType.APPLICATION_JSON)
    public boolean login(String username, String password) 
    {
        String clientID = UUID.randomUUID().toString();
        if(response == null) {
            System.out.println("response is null @o@.");
        }
        else {
            try {
                System.out.println("response is not null ^_^.");
                synchronized (this) {
                    System.out.println("response status: " + response.getStatus());
                    if(response.getOutputHeaders() == null) {
                        System.out.println("responsegetOutputHeaders() is null 0_0.");
                    }
                    else {
                        System.out.println("responsegetOutputHeaders() is not null ^_^.");
                        // Null Pointer Exception occurs here.
                        response.getOutputHeaders().putSingle("clientID", clientID);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }               
        }
    }
}

任何想法都将受到高度赞赏。

编辑:堆栈跟踪(将问题修复为@PT114提供的解决方案后):

18:23:00,447 ERROR [stderr] (http--127.0.0.1-8180-1)    at com.server.loginws.LoginWSImplRest.login(LoginWSImplRest.java:93)
18:23:00,447 ERROR [stderr] (http--127.0.0.1-8180-1)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
18:23:00,447 ERROR [stderr] (http--127.0.0.1-8180-1)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
18:23:00,447 ERROR [stderr] (http--127.0.0.1-8180-1)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
18:23:00,447 ERROR [stderr] (http--127.0.0.1-8180-1)    at java.lang.reflect.Method.invoke(Method.java:597)
18:23:00,447 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:155)
18:23:00,447 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:257)
18:23:00,447 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:222)
18:23:00,447 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:211)
18:23:00,447 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:525)
18:23:00,447 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:502)
18:23:00,447 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119)
18:23:00,463 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContain
erDispatcher.java:208)
18:23:00,463 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatch
er.java:55)
18:23:00,463 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatch
er.java:50)
18:23:00,463 ERROR [stderr] (http--127.0.0.1-8180-1)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
18:23:00,463 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.jav
a:329)
18:23:00,463 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
18:23:00,463 ERROR [stderr] (http--127.0.0.1-8180-1)    at com.server.loginws.DoServletFilter.doFilter(DoServletFilter.java:32)
18:23:00,463 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.jav
a:280)
18:23:00,478 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
18:23:00,478 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
18:23:00,478 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
18:23:00,478 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociation
Valve.java:153)
18:23:00,478 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
18:23:00,478 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
18:23:00,478 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
18:23:00,478 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
18:23:00,478 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
18:23:00,478 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java
:671)
18:23:00,478 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
18:23:00,478 ERROR [stderr] (http--127.0.0.1-8180-1)    at java.lang.Thread.run(Thread.java:662)
18:23:00,478 ERROR [stderr] (http--127.0.0.1-8180-1) org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
18:23:00,494 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.SynchronousDispatcher.handleApplicationException(SynchronousDispatc
her.java:340)
18:23:00,494 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:21
4)
18:23:00,494 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.
java:190)
18:23:00,494 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:540)
18:23:00,494 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:502)
18:23:00,509 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119)
18:23:00,509 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContain
erDispatcher.java:208)
18:23:00,509 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatch
er.java:55)
18:23:00,509 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatch
er.java:50)
18:23:00,509 ERROR [stderr] (http--127.0.0.1-8180-1)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
18:23:00,509 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.jav
a:329)
18:23:00,509 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
18:23:00,509 ERROR [stderr] (http--127.0.0.1-8180-1)    at com.server.loginws.DoServletFilter.doFilter(DoServletFilter.java:32)
18:23:00,509 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.jav
a:280)
18:23:00,509 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
18:23:00,509 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
18:23:00,509 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
18:23:00,509 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociation
Valve.java:153)
18:23:00,525 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
18:23:00,525 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
18:23:00,525 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
18:23:00,525 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
18:23:00,525 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
18:23:00,525 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java
:671)
18:23:00,525 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
18:23:00,525 ERROR [stderr] (http--127.0.0.1-8180-1)    at java.lang.Thread.run(Thread.java:662)
18:23:00,525 ERROR [stderr] (http--127.0.0.1-8180-1) Caused by: java.lang.NullPointerException
18:23:00,525 ERROR [stderr] (http--127.0.0.1-8180-1)    at com.server.loginws.LoginWSImplRest.login(LoginWSImplRest.java:101)
18:23:00,525 ERROR [stderr] (http--127.0.0.1-8180-1)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
18:23:00,525 ERROR [stderr] (http--127.0.0.1-8180-1)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
18:23:00,525 ERROR [stderr] (http--127.0.0.1-8180-1)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
18:23:00,525 ERROR [stderr] (http--127.0.0.1-8180-1)    at java.lang.reflect.Method.invoke(Method.java:597)
18:23:00,525 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:155)
18:23:00,541 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:257)
18:23:00,541 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:222)
18:23:00,541 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:211)
18:23:00,541 ERROR [stderr] (http--127.0.0.1-8180-1)    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:525)
18:23:00,541 ERROR [stderr] (http--127.0.0.1-8180-1)    ... 22 more
4

1 回答 1

1
    if(response != null) {
        System.out.println("response is null @o@.");
    }

这不是错误的方式吗?

    if(response == null) {
        System.out.println("response is null @o@.");
    } 
    else {
        stuff ...
    }
于 2012-10-16T11:05:45.130 回答