读书人

WebService之CXF容易使用

发布时间: 2012-11-04 10:42:42 作者: rapoo

WebService之CXF简单使用

服务端:

?

接口public interface ServerInter {void sayWord(String word);}   

?实现类

?

@WebServicepublic class ServerInterImpl implements ServerInter {public void sayWord(String word) {System.out.println(word);}}

?spring配置

?

<?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.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"/><!-- 普通bean --><bean id="serverService" serviceaddress="/serverWebService"><jaxws:serviceBean><ref bean = "serverService"/><!-- 暴露的bean的引用 --></jaxws:serviceBean></jaxws:server></beans>

?

?web.xml

?

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:/applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><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><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>

?

? 客户端

?

@WebServicepublic interface ServerInter {void sayWord(String word);}

?

测试类?

public class Test {

//1.通过spring配置客户端beanpublic void app1(){ClassPathXmlApplicationContext resource = new ClassPathXmlApplicationContext("applicationContext.xml");ServerInter serverInter = (ServerInter)resource.getBean("clientWebService");serverInter.sayWord("aaaaa");}//2.由JaxWsProxyFactoryBean创建bean,不需要在spring中配置public void app2(){String address = "http://localhost:8080/WebServiceServer/serverWebService";JaxWsProxyFactoryBean jwpf = new JaxWsProxyFactoryBean();jwpf.setAddress(address);jwpf.setServiceClass(ServerInter.class);ServerInter si = (ServerInter) jwpf.create();si.sayWord("bbbbb");}public static void main(String[] args) {Test test = new Test();test.app1();test.app2();}}

spring配置

<?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.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"/><!-- 注意客户端的接口名称及目录保持与服务端一致 --><!-- address即为访问的地址,其中的serverWebService即为服务端暴露的地址 --><jaxws:client id="clientWebService" serviceaddress="http://localhost:8080/WebServiceServer/serverWebService"></jaxws:client></beans>
?

读书人网 >Web前端

热点推荐