WebService(3)cxf webService與Spring整合

一、cxf webService 非Spring用例

(1)server端

1.引入jar包,pom.xml中配置如下:

<properties>
		<spring.version>4.1.3.RELEASE</spring.version>
		<cxf.version>3.0.3</cxf.version>
	</properties>

	<dependencies>
		<!-- Spring Web MVC Features -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
			<exclusions>
				<!-- Exclude Commons Logging in favor of SLF4j -->
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<!-- cxf -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>${cxf.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>${cxf.version}</version>
		</dependency>

		<!-- cxf -->

	</dependencies>
2.新建一個服務接口

@WebService
public interface IHelloWorld {
	
	String  sayHello(String name);

}
3.新建接口實現類

@WebService(endpointInterface="com.sf.interfaces.IHelloWorld",serviceName="IHelloWorld")
public class HelloWorldImpl implements IHelloWorld {

	public String sayHello(String name) {
		return name + ",你好呀";
	}
}
4.創建發佈service服務類

public class webServiceApp {

	public static void main(String[] args) {
		System.out.println("web service start");
		HelloWorldImpl implementor = new HelloWorldImpl();
		String address = "http://localhost:8080/IHelloWorld";
		Endpoint.publish(address, implementor);
		System.out.println("web service started");
	}
}
運行webServiceApp類,在瀏覽器中輸入:http://localhost:8080/IHelloWorld?wsdl,可以看到如下信息

<wsdl:definitions name="IHelloWorld" targetNamespace="http://impl.interfaces.sf.com/"><wsdl:import location="http://localhost:8080/IHelloWorld?wsdl=IHelloWorld.wsdl" namespace="http://interfaces.sf.com/">
    </wsdl:import><wsdl:binding name="IHelloWorldSoapBinding" type="ns1:IHelloWorld"><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="IHelloWorld"><wsdl:port binding="tns:IHelloWorldSoapBinding" name="HelloWorldImplPort"><soap:address location="http://localhost:8080/IHelloWorld"/></wsdl:port></wsdl:service></wsdl:definitions>
說明我們發佈成功了

(2)客戶端

客戶端調用,新建webServiceClient類

public class webServiceClient {
	public static void main(String[] args) {
		 JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
         svr.setServiceClass(IHelloWorld.class);
         svr.setAddress("http://localhost:8080/IHelloWorld");
         IHelloWorld hw = (IHelloWorld) svr.create();
         System.out.println(hw.sayHello("特朗普"));
	}
}
運行結果如下:
特朗普,你好呀

二、cxf webService 與Spring的整合

(1)server端的整合

1.applicationContext.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	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://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
	http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

	<jaxws:endpoint id="IHelloWorld"
		implementor="com.sf.interfaces.impl.HelloWorldImpl" address="/IHelloWorld" />

</beans>
2.web.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
	version="2.4">
	<display-name></display-name>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
		classpath:applicationContext.xml
		</param-value>
	</context-param>
	
	<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>/*</url-pattern>
	</servlet-mapping>


</web-app>
到此服務器端基本上告一段落,可以將應用部署到tomcat,啓動並訪問http://localhost:8080/項目名稱/IHelloWorld?wsdl,如果能正確顯示xml文件則說明部署成功。

(2)client端的整合

1.引入相應的jar包,同server端

2.建立跟server端一致的接口:IHelloWorld

3.applicationContext.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	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://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
	http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

	<bean id="client" class="com.sf.interfaces.IHelloWorld"
		factory-bean="clientFactory" factory-method="create" />

	<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
		<property name="serviceClass" value="com.sf.interfaces.IHelloWorld" />
		<property name="address" value="http://localhost:8080/IHelloWorld" />
	</bean>

</beans>
4.建立測試類:

package com.sf.interfaces.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sf.interfaces.IHelloWorld;

public class CxfClientTest {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		IHelloWorld client = (IHelloWorld) ctx.getBean("client");
		String result = client.sayHello("普京");
		System.out.println(result);
	}

}
執行結果:普京,你好呀











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