cxf + spring 集成开发webservice

1. 用cxf + spring 开发webservice

/**
 * @author liuqing
 * 
 */
package demo.hw.server;

import javax.jws.WebService;


/**
 * webservice 接口
 * @author liuqing
 *
 */
@WebService
public interface HelloWorld {
    
    String sayHello(String text);
}

2. cxf 接口实现类

 

package demo.hw.server;


/**
 * 
 * @author liuqing
 * webservice 实现类
 */
public class HelloWorldImpl implements HelloWorld {

    public String sayHello(String text) {
        System.out.println("sayHi called");
        return "Hello " + text;
    }
}

  3. 配置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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>cxfspring</display-name>
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/beans.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
     <servlet>
         <servlet-name>CXFServlet</servlet-name>
         <servlet-class>
              org.apache.cxf.transport.servlet.CXFServlet
         </servlet-class>
     </servlet>
     <servlet-mapping>
         <servlet-name>CXFServlet</servlet-name>
         <url-pattern>/services/*</url-pattern>
     </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

4. spring beans.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

	<jaxws:endpoint 
	  id="helloWorld" 
	  implementor="demo.hw.server.HelloWorldImpl" 
	  address="/HelloWorld" />
	
</beans>
<!-- END SNIPPET: beans -->

5. 启动tomcat

2011-6-2 17:06:31 org.apache.cxf.transport.servlet.CXFServlet updateContext
信息: Load the bus with application context
2011-6-2 17:06:31 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.apache.cxf.bus.spring.BusApplicationContext@1971eb3: startup date [Thu Jun 02 17:06:31 CST 2011]; parent: Root WebApplicationContext
2011-6-2 17:06:31 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@c28cb7: defining beans []; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@1ff92f5
2011-6-2 17:06:31 org.apache.cxf.transport.servlet.AbstractCXFServlet replaceDestinationFactory
信息: Servlet transport factory already registered 

6. 生成wsdl 文件

  <?xml version="1.0" encoding="UTF-8" ?> 
- <wsdl:definitions name="HelloWorldImplService" targetNamespace="http://server.hw.demo/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.hw.demo/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <wsdl:types>
- <xs:schema elementFormDefault="unqualified" targetNamespace="http://server.hw.demo/" version="1.0" xmlns:tns="http://server.hw.demo/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="sayHello" type="tns:sayHello" /> 
  <xs:element name="sayHelloResponse" type="tns:sayHelloResponse" /> 
- <xs:complexType name="sayHello">
- <xs:sequence>
  <xs:element minOccurs="0" name="arg0" type="xs:string" /> 
  </xs:sequence>
  </xs:complexType>
- <xs:complexType name="sayHelloResponse">
- <xs:sequence>
  <xs:element minOccurs="0" name="return" type="xs:string" /> 
  </xs:sequence>
  </xs:complexType>
  </xs:schema>
  </wsdl:types>
- <wsdl:message name="sayHelloResponse">
  <wsdl:part element="tns:sayHelloResponse" name="parameters" /> 
  </wsdl:message>
- <wsdl:message name="sayHello">
  <wsdl:part element="tns:sayHello" name="parameters" /> 
  </wsdl:message>
- <wsdl:portType name="HelloWorld">
- <wsdl:operation name="sayHello">
  <wsdl:input message="tns:sayHello" name="sayHello" /> 
  <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorld">
  <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
- <wsdl:operation name="sayHello">
  <soap:operation soapAction="" style="document" /> 
- <wsdl:input name="sayHello">
  <soap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output name="sayHelloResponse">
  <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="HelloWorldImplService">
- <wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort">
  <soap:address location="http://localhost:9090/cxfspring/services/HelloWorld" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

猜你喜欢

转载自mianhuaman.iteye.com/blog/1068718