代码之家  ›  专栏  ›  技术社区  ›  seiti

Java EE Web服务:@AroundInvoke不工作

  •  4
  • seiti  · 技术社区  · 10 年前

    我有一个关于@AroundInvoke for Java EE Web服务的问题: 我使用这个类作为REST服务的侦听器(它正在工作):

    public class AuthenticationInterceptor {
    
      @AroundInvoke
      public Object invoke(InvocationContext ctx) throws Exception {
        if (ctx != null) {
          // do stuff ...
          return ctx.proceed();
        } else {
          return null;
        }
      }
    
    }
    

    我现在尝试将这个类用于我实现的Web服务(SOAP)。出于调试目的,我创建了一个缩减的项目,它只有一个类:

    @WebService
    @Interceptors(AuthenticationInterceptor.class)
    public class HelloWorld {
    
      @WebMethod
      @Interceptors(AuthenticationInterceptor.class)
      public String sayHello(String name) {
        return "Hello " + (name == null ? "" : name) + " !";
      }
    
    }
    

    当我在glassfish服务器(3.1.1)上部署生成的.war文件时,Web服务可用,但使用远程调试时,不会调用带注释的方法调用(与使用REST服务时不同)。编程环境:

    • 日食
    • Maven(依赖项:glassfish嵌入式all、javaeeapi、junit)
    • Glassfish服务器3.1.1

    记录在案:我也很想将beans.xml添加到项目中(尽管这对我的REST项目来说不是必要的)-因为这个xml文件glassfish抛出了一个: 部署期间出错:关闭应用程序容器时出现异常:java.lang.NullPointerException

    beans.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
           http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
           bean-discovery-mode="all">
    
           <interceptors>
            <class>com.sjd.simple.AuthenticationInterceptor</class>
           </interceptors>
    </beans>
    

    (如果没有beans.xml,它不应该像我的其他项目一样工作吗?)。有人知道我的带注释的@AroundInvoke方法没有被调用的原因吗?

    1 回复  |  直到 10 年前
        1
  •  4
  •   seiti    10 年前

    我自己解决了这个问题。根据我在互联网上的研究/收集:

    javax.interceptor.interceptor可以绑定到一个简单的Web Bean, 企业Web Bean或EJB3风格的会话、单例或消息 使用javax.interceptor.Interceptors注释或 使用WebBeans拦截器绑定。

    当不使用JBoss时,这似乎也是正确的。问题是,每次我将@Stateless添加到HelloWorld类时,我都无法使用添加@Stateless注释之前使用的URL访问Web服务。因为我看到了@Stateless和@Webservice一起使用的其他示例,所以我深入研究了一下,发现了这个bug报告:

    @WebService and @Stateless not showing endpoint in GlassFish 4.0 admin console

    似乎通过使用@Stateless注释,Web服务url从以下更改:

    http://localhost:8080/<application_name>/<class_name>Service?wsdl      to
    http://localhost:8080/<class_name>Service/<class_name>?wsdl
    

    在我的示例中:

    http://localhost:8080/simple-jax-ws/HelloWorldService?wsdl    [without @Stateless]
    http://localhost:8080/HelloWorldService/HelloWorld?wsdl       [with @Stateless]
    

    我希望这个答案也能帮助其他人。我还写了一篇博客,以防有人在搜索我使用的源代码: blog post