当前位置: 代码迷 >> Web前端 >> CXF 聚合spring开发webservice
  详细解决方案

CXF 聚合spring开发webservice

热度:272   发布时间:2013-03-16 11:51:46.0
CXF 集合spring开发webservice
一,首先是准备工作,所需要的jar包:

cxf-2.4.1.jar
neethi-3.0.0.jar
xmlschema-core-2.0.jar
wsdl4j-1.6.2.jar

geronimo-activation_1.1_spec-1.1.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-javamail_1.4_spec-1.7.1.jar
geronimo-servlet_3.0_spec-1.0.jar
geronimo-ws-metadata_2.0_spec-1.1.3.jar
jaxb-api-2.2.1.jar
jaxb-impl-2.2.1.1.jar
jaxb-xjc-2.2.1.1.jar
jetty-http-7.4.2.v20110526.jar
jetty-server-7.4.2.v20110526.jar
jetty-util-7.4.2.v20110526.jar
saaj-api-1.3.jar
saaj-impl-1.3.2.jar
wss4j-1.6.1.jar
xmlbeans-2.4.0.jar


红色部分是不集成spring开发调试需要用到的jar包,spring需要的包一般spring工程都会有,这里不做说明。

二,创建service接口和实现类


接口:
@WebService
public interface HelloWorld {
	
	String sayHello(@WebParam(name="text") String text);

}


实现类:
@WebService(endpointInterface="com.***.***.webservice.test.HelloWorld",serviceName="helloword" )
@Component(value="helloWorld")
public class HelloWordImpl implements HelloWorld {

	
	public String sayHello(String text) {
		
		return "Hello, "+text;
	}

}


二,配置web.xml过滤.
在工程的web.xml添加如下内容:

<servlet>
     <servlet-name>CXFServlet</servlet-name>
     <display-name>CXFServlet</display-name>
     <servlet-class>
       org.apache.cxf.transport.servlet.CXFServlet
     </servlet-class>
  </servlet>
  <servlet-mapping>
     <servlet-name>CXFServlet</servlet-name>
     <url-pattern>/webservice/*</url-pattern>
  </servlet-mapping>


三,集成到spring,定义bean及访问服务的URL等,通过spring发布服务。

在这为使得配置清晰分明,新建一个applicationContext-cxf.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-3.0.xsd
                       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.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="sayhello"
           implementor="#helloWorld"
           address="/helloWorld" />

     <jaxws:client id="client"     
        serviceClass="com.***.**.webservice.test.HelloWorld"     
        address="http://localhost:8080/webservice/helloWorld" />
      
</beans>


将上面的文件引入到applicationContext.xml中:
<import resource="applicationContext-cxf.xml"/>


启动服务,在地址栏访问 http://localhost:8080/webservice/helloWorld?wsdl,若成功,则能看到一段wsdl的xml信息。

客服端根据wsdl的信息生成客户端的代码。

非集成spring发布服务代码如下:

import javax.xml.ws.Endpoint;

import com.***.***.webservice.test.impl.HelloWordImpl;

public class WebServiceApp {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		System.out.println("web service start");
        HelloWordImpl implementor= new HelloWordImpl();
        String address="http://localhost:8080/helloWorld";
        Endpoint.publish(address, implementor);
        System.out.println("web service started");

	}

}


  相关解决方案