1

我试图通过扩展 SpringBeanAutowiringSupport 来公开我的 Web 服务。我正在使用 GlassFish 3.1 和 Spring 3.0.5.RELEASE。

我在这里学习教程。

但是,当我转到我的 Web 服务的 URL 时,我得到了 ClassCastException。

关于我缺少什么或做错了什么的任何想法?

WARNING: StandardWrapperValve[GreetingService]: PWC1382: Allocate exception for servlet GreetingService
java.lang.ClassCastException: com.service.GreetingServiceEndpoint cannot be cast to javax.servlet.Servlet
    at com.sun.enterprise.web.WebContainer.createServletInstance(WebContainer.java:702)
    at com.sun.enterprise.web.WebModule.createServletInstance(WebModule.java:1958)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1263)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:1070)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:189)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:680)

端点

@WebService (serviceName="GreetingService")
public class GreetingServiceEndpoint extends SpringBeanAutowiringSupport {
    @Autowired
    private GreetingService greetingService;

    @WebMethod
    public String sayHello () {
        return greetingService.sayHello();
    }
}

服务

@Service ("greetingService")
public class GreetingService {
    public String sayHello () {
        return "Hello from Greeting Service";
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0" metadata-complete="true">  

    <display-name>Archetype Created Web Application</display-name>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>GreetingService</servlet-name>
    <servlet-class>com.service.GreetingServiceEndpoint</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>GreetingService</servlet-name>
    <url-pattern>/GreetingService</url-pattern>
  </servlet-mapping>
</web-app>
4

2 回答 2

2

你的类:com.service.GreetingServiceEndpoint不是javax.servlet.Servlet. 您不能将其web.xml作为 servlet 放入。要运行JAX-WSWeb 服务,JAX-WS必须在 web.xml 中声明 servlet:com.sun.xml.ws.transport.http.servlet.WSServlet该 servlet 将为您的 Web 服务提供请求。

完整的 servlet 声明应如下所示:

<servlet>
    <servlet-name>Greeting</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>Greeting</servlet-name>
    <url-pattern>/services/Greeting</url-pattern>
</servlet-mapping>

您的com.service.GreetingServiceEndpoint类应该在sun-jaxws.xml文件中定义,如下所示:

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

必须将此文件添加到WEB-INF您的 Web 应用程序中。看看我的帖子在这里。应该会有所帮助。

于 2012-10-12T10:16:55.117 回答
0

无需在;中注册com.service.GreetingServiceEndpoint为 Servlet web.xml因为它没有扩展 HttpServlet。

于 2012-10-12T01:57:29.027 回答