WebService(3)cxf webService与Spring整合

来源:互联网 发布:vmtools mac 10.11 编辑:程序博客网 时间:2024/06/15 00:15

一、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.新建一个服务接口

@WebServicepublic 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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://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);}}
执行结果:普京,你好呀











0 0