您的位置:首页 > 编程语言 > Java开发

CXF与Spring的集成demo

2014-12-23 14:46 176 查看
一.准备工作

下载apache-cxf应用包,我目前使用的是apache-cxf-2.7.5

二.定义服务接口

package com.ws;

import javax.jws.WebMethod;

import javax.jws.WebParam;

import javax.jws.WebService;

import javax.jws.soap.SOAPBinding;

import javax.jws.soap.SOAPBinding.Style;

/**

*

* @author guoming

*

*/

@WebService

@SOAPBinding(style=Style.RPC)

public interface GreetingI {

@WebMethod(operationName="toSay")

public void say(@WebParam(name="userName")String userName);

}

---------------------------------------------------------------------------------------------------------------------------------------

package com.ws;

import javax.jws.WebService;

import javax.jws.soap.SOAPBinding;

import javax.jws.soap.SOAPBinding.Style;

/**

*

* @author guoming

*

*/

@WebService(endpointInterface="com.ws.GreetingI")

@SOAPBinding(style=Style.RPC)

public class GreetingImpl implements GreetingI {

public GreetingImpl(){}

@Override

public void say(String userName) {

// TODO Auto-generated method stub

System.out.println("Hi:"+userName);

}

}

三.在spring上下文中application.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:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:tx="http://www.springframework.org/schema/tx"

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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- cxf配置 -->

<import resource="classpath*:META-INF/cxf.xml"/>

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

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

<bean id="greetingServiceBean" class="com.ws.GreetingImpl"></bean>

<jaxws:endpoint id="greetingService" implementor="#greetingServiceBean" address="/greeting" />

</beans>

四.修改web.xml

<servlet>

<servlet-name>CXFService</servlet-name>

<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>CXFService</servlet-name>

<url-pattern>/cxf/*</url-pattern>

</servlet-mapping>

五.启动tomcat,访问 http://localhost:8088/HibernateAnnotation/cxf/greeting?wsdl

出现:

该 XML 文件并未包含任何关联的样式信息。文档树显示如下。

<wsdl:definitions name="GreetingImplService" targetNamespace="http://ws.com/"><wsdl:message name="toSay"><wsdl:part name="userName" type="xsd:string">

</wsdl:part></wsdl:message><wsdl:message name="toSayResponse">

</wsdl:message><wsdl:portType name="GreetingI"><wsdl:operation name="toSay"><wsdl:input message="tns:toSay" name="toSay">

</wsdl:input><wsdl:output message="tns:toSayResponse" name="toSayResponse">

</wsdl:output></wsdl:operation></wsdl:portType><wsdl:binding name="GreetingImplServiceSoapBinding" type="tns:GreetingI"><soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/><wsdl:operation name="toSay"><soap:operation soapAction=""
style="rpc"/><wsdl:input name="toSay"><soap:body namespace="http://ws.com/" use="literal"/></wsdl:input><wsdl:output name="toSayResponse"><soap:body namespace="http://ws.com/" use="literal"/></wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="GreetingImplService"><wsdl:port
binding="tns:GreetingImplServiceSoapBinding" name="GreetingImplPort"><soap:address location="http://localhost:8088/HibernateAnnotation/cxf/greeting"/></wsdl:port></wsdl:service></wsdl:definitions>


说明服务发布成功!

创建一个客户端来调用webservice

1.新建一个java project,并加入cxf相应的包

2.将webservice的接口类导出成jar包,也添加到Build Path,主要目的是客户端要用到服务端的GreetingI这个接口。如果不想导入这个jar包也可以,只要在客户端创建一个一摸一样的接口类:GreetingI,特别要注意以下两点:

1)接口前面要添加@Webservice的标记,不然会抛出一个 javax.xml.ws.WebServiceException: Could not find wsdl:binding operation info for web method sayHelloWorld.

2)包路径也要一样,不然会抛出一个ClassCastException: $Proxy29 cannot be cast to...

3.配置spring-client.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:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:tx="http://www.springframework.org/schema/tx"

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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean" p:serviceClass="com.ws.GreetingI"

p:address="http://localhost:8088/HibernateAnnotation/cxf/greeting"

/>

<bean id="client" factory-bean="clientFactory" factory-method="create"/>

</beans>

4.测试

public static void main(String[] args) {

ApplicationContext ctx=new ClassPathXmlApplicationContext("basic.xml");

com.ws.GreetingI greet=null;

try {

greet = (com.ws.GreetingI)ctx.getBean("client");

} catch (Exception e) {

System.out.println("error...........");

e.printStackTrace();

}

if (null != greet) {

String result=greet.say("lisi");

System.out.println(result);

}

输出:Hi:lisi

测试通过,至此一个webservice的调用也结束了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: