Spring集成CXF

直接代碼

服務器端

定義接口和實現類

IHelloWorld.java

package com.sf.module.lscmcommon.webservice;

import java.util.List;
import javax.jws.WebService;

@WebService
public interface IHelloWorld {

	public String sayHello(String text);

	public List<String> findObj();

}

HelloWorld.java

package com.sf.module.lscmcommon.webservice;

import java.util.List;

import javax.jws.WebService;

@WebService
public class HelloWorld implements IHelloWorld {

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

	public List<String> findObj(){
		return null;
	}
}

配置Spring的xml文件

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd     
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd     
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://cxf.apache.org/jaxws 
http://cxf.apache.org/schemas/jaxws.xsd"

xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws">
    <!-- 這三個必加 -->
    <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" /> 
    
	
	<!-- cxf webService 接口實現類 -->
	<bean id="hello" class="com.sf.module.lscmcommon.webservice.HelloWorld" /> 
	<!-- 
		id:指在spring配置的bean的ID.
		Implementor:指明具體的實現類.
		Address:指明這個web service的相對地址,如:http://10.0.7.115:8080/lscm/ws/IHelloWorld?wsdl
	-->
    <jaxws:endpoint id="helloWorld" implementor="#hello" 
        address="/IHelloWorld" /> 
        
	
</beans>

在web.xml裏面

追加

...	<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>/ws/*</url-pattern>
	</servlet-mapping>	...

服務端完成,部署tomcat,打開地址.出現這玩意就是成功了.



客戶端開發

新建一個工程,定義一個接口即可,和服務端的一致.

IHelloWorld .java

package com.cdg.webService;

import javax.jws.WebService;

@WebService
public interface IHelloWorld {
	public String sayHello(String text);  
}


定義Spring 的.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-2.0.xsd     
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-2.0.xsd     
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
		http://cxf.apache.org/jaxws 
		http://cxf.apache.org/schemas/jaxws.xsd"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jaxws="http://cxf.apache.org/jaxws">

	<!-- 
		id:不需要我解釋
		serviceClass:接口類包名
		address:服務器的地址,後面的?wsdl不要
	 -->
	<jaxws:client id="helloWorld"
		serviceClass="com.cdg.webService.IHelloWorld"
		address="http://10.0.7.115:8080/lscm/ws/IHelloWorld" />

	<!-- 加載數據源 -->
	<import resource="db-config.xml" />
</beans>

其他的都不用了;

編寫個測試類看看有沒有連接成功

package com.cdg.webService;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloWorld {

	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"classpath:springConfig/applicationContext.xml");
		IHelloWorld client = (IHelloWorld) ctx.getBean("helloWorld");
		String result = client.sayHello("你好!");
		System.out.println("回調 : " + result);
	}
}

以上的包自己去下載.

溫馨提示:如果有報JAXB的包的錯誤,就看是不是和jdk版本有衝突,換一下jaxb的版本即可.

看效果===================================================================





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章