【问题标题】:Glassfish web.xml servlet mapping to @WebService gets ClassCastExceptionGlassfish web.xml servlet 映射到 @WebService 获取 ClassCastException
【发布时间】:2013-03-01 11:02:33
【问题描述】:

我试图在部署到 Glassfish(最好也部署到 TomEE)时控制我的 Web 服务的 URL 端点。

我有一堂课:

@Stateless
@WebService(
    targetNamespace = "http://foo.net/doc/2012-08-01",
    name = "FooService",
    portName = "FooPort",
    serviceName = "FooService")
public class FooSoapService extends SoapBase {
...
}

还有一个 web.xml:

<servlet>
    <description>SOAP Endpoint for Foo operations.</description>
    <servlet-name>Foo</servlet-name>
    <servlet-class>com.foo.FooSoapService</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>FooPack</servlet-name>
    <url-pattern>/soap/FooPack</url-pattern>
</servlet-mapping>

如果我在 Glassfish 中部署时访问 /context-root/soap/FooPack?wsdl,我最终会得到:

java.lang.ClassCastException: com.foo.FooSoapService cannot be cast to javax.servlet.Servlet

除了一些 jax-rs 的东西之外,web.xml 中几乎没有其他内容。

有什么想法吗?

【问题讨论】:

    标签: jakarta-ee servlets glassfish jax-ws


    【解决方案1】:

    嗯,FooSoapService 类,您声称是 Web 服务实现类,需要实现服务接口,可能是在您的 @WebService 注释 serviceName 属性中定义的 FooService。

    您收到此异常的原因是因为您的 FooSoapService 类不是 javax.servlet.Servlet 的实例,并且可以肯定它不需要是一个实例。在您的 web.xml 中,您不能公开您的 Web 服务端点。需要通过sun-jaxws.xml 完成。像这样的:

    <endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
         <endpoint name="FooPort" implementation="com.foo.FooSoapService" url-pattern="/services/FooService"/>
    </endpoints>
    

    您的 web.xml 应该如下所示:

    <listener>
        <listener-class>
                com.sun.xml.ws.transport.http.servlet.WSServletContextListener
        </listener-class>
    </listener>
    <servlet>
        <servlet-name>Foo</servlet-name>
        <servlet-class>
            com.sun.xml.ws.transport.http.servlet.WSServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Foo</servlet-name>
        <url-pattern>/services/FooService</url-pattern>
    </servlet-mapping>
    

    如果您进行这些更改,那么您将能够从以下位置获取 WSDL:

    /context-root/services/FooService?wsdl
    

    【讨论】:

    • 这可能在部署到 TomEE 时不起作用?我的目标是兼容性,在尝试决定在生产中使用哪个时尤其重要(我们甚至可以同时使用两者)>
    • 为什么你认为它在 TomEE 中不起作用?这是创建基于 JAX-WS 的 Web 服务的一般方法。这不是解决方法之王之类的,它只是它应该是并且应该在所有支持 JAX-WS 的容器上工作。
    • 因为 TomEE 没有 com.sun.xml.ws.transport.http.servlet.WSServletContextListener(参见cxf.apache.org/javadoc/latest)。
    • 那么问题出在哪里?在你的类路径中添加jaxws-rt-x.x.x.jar,就是这样。从您提供的链接中,我假设您正在使用 CXF,那么为什么不使用 CXF 方法来公开 Web 服务呢?
    • 事实证明,要构建同时支持两者的构建非常困难。坦率地说,供应商中立的部署描述符包含供应商特定的引用以使其工作,这令人失望。它最终看起来也非常复杂,并且添加另一个应用程序服务器,您最终可能也需要对其进行配置。
    【解决方案2】:

    glassfish 4.0 也具有此功能。配置可部署且没有错误。

    【讨论】:

      猜你喜欢
      • 2015-03-01
      • 2013-01-20
      • 2012-07-23
      • 2016-07-05
      • 2012-04-17
      • 2012-01-02
      • 2016-02-05
      • 2017-11-05
      相关资源
      最近更新 更多