WEBSERVICE之CXF服务端与客户端配置(初级应用,本人才疏学浅啊)
2013-04-03 10:01
417 查看
CXF实现与Spring整合访问,实现客户端与服务端之间的通信:通信过程中的bean必须序列化,才能在网络中传输
客户端:
一、通过手动+配置方式实现:
配置web.xml:如果只作为客户端,只需要配置加载spring配置文件部分即可,当作为服务端时,才需要<!-- webservice 配置 -->配置部分
<!--Spring ApplicationContext 载入 -->
<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>
<!-- webservice 配置
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
貌似是可有可无
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
-->
配置applicationContext-ws-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:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-autowire="byType" default-lazy-init="true">
<description>WebService客户端配置</description>
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<!--
<jaxws:client id="userService" serviceClass="com.creditease.oa.manager.IUserWebService"
address="${smp.url}/services/userService">
</jaxws:client>
-->
<!--
在客户端一方,获得一个该接口声明,调用接口方法,通过配置文件,找到address服务端对应的接口
address为服务地址
-->
<jaxws:client id="capticalAssetService" serviceClass="com.creditease.oa.manager.ICapticalAssetService"
address="http://10.105.18.96:8081/easext/webservices/capticalAsset">
</jaxws:client>
</beans>
客户端实现类获取服务接口的方式:
//ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext-ws-client.xml" });
//ICapticalAssetService capticalAssetService = (ICapticalAssetService) context.getBean("capticalAssetService");
二、通过手动+不配置方式:
public class ITestServiceTest {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(ITestService.class);
factory.setAddress("http://localhost:8080/webServicecxf/webservices/testService");
客户端实现类获取服务接口
ITestService service = (ITestService)factory.create();
String result = service.helloWord("good");
}
}
三、通过自动生成方式:通过myeclipse自动生成客户端
生成的客户端文件包名为服务端接口实现类定义的命名空间名
客户端生成的服务接口提供类为:名称=targetNamespace命名空间名+portName+Service,这个类会继承Service类
public class AutoTest {
public static void main(String[] args) {
//继承了Service的类new一个对象
TestServiceImplService ts = new TestServiceImplService();
//客户端实现类获取服务接口,如果服务端接口实现类配置了portName="serviceImpl",则getPort()为getServiceImpl()
ITestService service = ts.getPort();
String str = service.helloWord("ok");
}
}
方式一、方式二需要在客户端建立与服务端相同的接口、bean以及提供相关的jar包,方式三会自动生成接口文件以及bean文件
@WebService(targetNamespace="payment")
public interface IPaymentService {
public List<CapticalAssetsBean> queryAssetsClassNameAndCodeByFistClass(String first_class);
public List<CapticalAssetsBean> queryMeasureUnitInfo();
}
public class CapticalAssetsBean implements Serializable {
private String name;
private String code;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
服务端:需要配置web.xml、applicationContext*.xml
配置web.xml:
<!--Spring ApplicationContext 载入 -->
<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>
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext*.xml</param-value>
</context-param>
<!-- webservice 配置
-->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<!-- 貌似是可有可无
-->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
配置applicationContext-ws-server.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: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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" >
<!--配置实现的原理:定义一个接口实现类bean,然后在配置一个终端,实现拦截的请求与此接口实现类连接-->
<bean id="_deptWebService" class="com.creditease.oa.manager.impl.DeptWebServiceImpl" />
<jaxws:endpoint id="deptService" address="/deptService">
<jaxws:implementor ref="_deptWebService" />
<!--
<jaxws:inInterceptors>
<bean class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
<property name="properties">
<map>
<entry key="action" value="UsernameToken" />
<entry key="passwordType" value="PasswordText" />
<entry key="passwordCallbackRef" value-ref="serverValidateCallback" />
</map>
</property>
</bean>
</jaxws:inInterceptors>
-->
</jaxws:endpoint>
<!-- 下面这两个bean以及参数??? -->
<bean id="aegisBean" class="org.apache.cxf.aegis.databinding.AegisDatabinding" />
<bean id="jaxWsServiceFactoryBean" class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean" scope="prototype">
<property name="wrapped" value="true" />
<property name="dataBinding" ref="aegisBean" />
</bean>
<!-- implementor="#myTestService"指向接口实现类bean(jquery中的$("#id")使用) ,address="/testService"就相当于action的name作用 -->
<bean id="myTestService" class="com.cxf.TestServiceImpl"></bean>
<jaxws:endpoint id="testService" implementor="#myTestService" address="/testService">
<jaxws:serviceFactory>
<ref bean="jaxWsServiceFactoryBean" />
</jaxws:serviceFactory>
</jaxws:endpoint>
<!-- 测试 第二种方式,通过Spring创建数据绑定的类现在一般都是使用这种方式-->
<bean id="my" class="com.cxf.MyTestServiceImpl"></bean>
<jaxws:server id="myWebService" serviceClass="com.cxf.IMyTestService" address="http://localhost:1111/services/testService">
<jaxws:serviceBean>
<ref bean="my"/>
</jaxws:serviceBean>
</jaxws:server>
<!-- 这里需要导入cxf的xml文件 -->
<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-extension-http-jetty.xml"/>
</beans>
请求url的组成部分:http://localhost:80/webServicecxf/webservices/testService
webServicecxf:是项目名
/webservices/:在服务端的web.xml文件中servlet的pattern-url拦截的就是/webservices/*
/testService:在服务端的applicationContext*.xml配置文件中,服务终端的address就是/testService
整个请求的流程走向:客户端进行业务操作,调用接口,在客户端的applicationContext*.xml配置文件中serviceClass定义的就是该接口,而address定义的服务端访问url
请求会被服务端的web.xml的pattern-url拦截,然后找到服务端的applicationContext*.xml文件,其中address为对应url的尾部,通过address找到
implementor所指向的服务接口实现类
客户端:
一、通过手动+配置方式实现:
配置web.xml:如果只作为客户端,只需要配置加载spring配置文件部分即可,当作为服务端时,才需要<!-- webservice 配置 -->配置部分
<!--Spring ApplicationContext 载入 -->
<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>
<!-- webservice 配置
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
貌似是可有可无
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
-->
配置applicationContext-ws-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:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-autowire="byType" default-lazy-init="true">
<description>WebService客户端配置</description>
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<!--
<jaxws:client id="userService" serviceClass="com.creditease.oa.manager.IUserWebService"
address="${smp.url}/services/userService">
</jaxws:client>
-->
<!--
在客户端一方,获得一个该接口声明,调用接口方法,通过配置文件,找到address服务端对应的接口
address为服务地址
-->
<jaxws:client id="capticalAssetService" serviceClass="com.creditease.oa.manager.ICapticalAssetService"
address="http://10.105.18.96:8081/easext/webservices/capticalAsset">
</jaxws:client>
</beans>
客户端实现类获取服务接口的方式:
//ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext-ws-client.xml" });
//ICapticalAssetService capticalAssetService = (ICapticalAssetService) context.getBean("capticalAssetService");
二、通过手动+不配置方式:
public class ITestServiceTest {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(ITestService.class);
factory.setAddress("http://localhost:8080/webServicecxf/webservices/testService");
客户端实现类获取服务接口
ITestService service = (ITestService)factory.create();
String result = service.helloWord("good");
}
}
三、通过自动生成方式:通过myeclipse自动生成客户端
生成的客户端文件包名为服务端接口实现类定义的命名空间名
客户端生成的服务接口提供类为:名称=targetNamespace命名空间名+portName+Service,这个类会继承Service类
public class AutoTest {
public static void main(String[] args) {
//继承了Service的类new一个对象
TestServiceImplService ts = new TestServiceImplService();
//客户端实现类获取服务接口,如果服务端接口实现类配置了portName="serviceImpl",则getPort()为getServiceImpl()
ITestService service = ts.getPort();
String str = service.helloWord("ok");
}
}
方式一、方式二需要在客户端建立与服务端相同的接口、bean以及提供相关的jar包,方式三会自动生成接口文件以及bean文件
@WebService(targetNamespace="payment")
public interface IPaymentService {
public List<CapticalAssetsBean> queryAssetsClassNameAndCodeByFistClass(String first_class);
public List<CapticalAssetsBean> queryMeasureUnitInfo();
}
public class CapticalAssetsBean implements Serializable {
private String name;
private String code;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
服务端:需要配置web.xml、applicationContext*.xml
配置web.xml:
<!--Spring ApplicationContext 载入 -->
<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>
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext*.xml</param-value>
</context-param>
<!-- webservice 配置
-->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<!-- 貌似是可有可无
-->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
配置applicationContext-ws-server.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: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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" >
<!--配置实现的原理:定义一个接口实现类bean,然后在配置一个终端,实现拦截的请求与此接口实现类连接-->
<bean id="_deptWebService" class="com.creditease.oa.manager.impl.DeptWebServiceImpl" />
<jaxws:endpoint id="deptService" address="/deptService">
<jaxws:implementor ref="_deptWebService" />
<!--
<jaxws:inInterceptors>
<bean class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
<property name="properties">
<map>
<entry key="action" value="UsernameToken" />
<entry key="passwordType" value="PasswordText" />
<entry key="passwordCallbackRef" value-ref="serverValidateCallback" />
</map>
</property>
</bean>
</jaxws:inInterceptors>
-->
</jaxws:endpoint>
<!-- 下面这两个bean以及参数??? -->
<bean id="aegisBean" class="org.apache.cxf.aegis.databinding.AegisDatabinding" />
<bean id="jaxWsServiceFactoryBean" class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean" scope="prototype">
<property name="wrapped" value="true" />
<property name="dataBinding" ref="aegisBean" />
</bean>
<!-- implementor="#myTestService"指向接口实现类bean(jquery中的$("#id")使用) ,address="/testService"就相当于action的name作用 -->
<bean id="myTestService" class="com.cxf.TestServiceImpl"></bean>
<jaxws:endpoint id="testService" implementor="#myTestService" address="/testService">
<jaxws:serviceFactory>
<ref bean="jaxWsServiceFactoryBean" />
</jaxws:serviceFactory>
</jaxws:endpoint>
<!-- 测试 第二种方式,通过Spring创建数据绑定的类现在一般都是使用这种方式-->
<bean id="my" class="com.cxf.MyTestServiceImpl"></bean>
<jaxws:server id="myWebService" serviceClass="com.cxf.IMyTestService" address="http://localhost:1111/services/testService">
<jaxws:serviceBean>
<ref bean="my"/>
</jaxws:serviceBean>
</jaxws:server>
<!-- 这里需要导入cxf的xml文件 -->
<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-extension-http-jetty.xml"/>
</beans>
请求url的组成部分:http://localhost:80/webServicecxf/webservices/testService
webServicecxf:是项目名
/webservices/:在服务端的web.xml文件中servlet的pattern-url拦截的就是/webservices/*
/testService:在服务端的applicationContext*.xml配置文件中,服务终端的address就是/testService
整个请求的流程走向:客户端进行业务操作,调用接口,在客户端的applicationContext*.xml配置文件中serviceClass定义的就是该接口,而address定义的服务端访问url
请求会被服务端的web.xml的pattern-url拦截,然后找到服务端的applicationContext*.xml文件,其中address为对应url的尾部,通过address找到
implementor所指向的服务接口实现类
相关文章推荐
- SpringCloud系列九:SpringCloudConfig 基础配置(SpringCloudConfig 的基本概念、配置 SpringCloudConfig 服务端、抓取配置文件信息、客户端使用 SpringCloudConfig 进行配置、单仓库目录匹配、应用仓库自动选择、仓库匹配模式)
- CXF框架的restful webservice 环境配置(二) 客户端
- CXF写SOAP WebService服务端与客户端
- WebService服务端与客户端的配置
- 开发webservice服务端和客户端(cxf,xfire)
- CentOS SVN 服务端 客户端 应用安装配置
- 应用Python开发WebService服务端及客户端
- 使用CXF开发WebService程序的总结(五):基于Map数据类型处理的的客户端和服务端代码的编写
- WebService-03-使用CXF开发服务端和客户端
- 客户端axis调用cxf服务端webservice接口问题
- Spring集成CXF搭建WebService服务端与客户端
- 一、使用cxf的JaxWsServerFactoryBean创建webservice的服务端和客户端
- 基于cxf框架的webservice服务端和客户端编写
- (转载)webservice 客户端,axis调用cxf 发布的webservices服务端
- WebService中CXF的简单的配置和应用(ssm框架)
- Webservice的cxf开发_使用spring配置客户端
- CXF和Spring整合实现WebService服务端、客户端
- 3、webService CXF的应用1 tomcat的配置方法
- CXF生成webservice服务端,httpclient客户端调用webservice服务端
- 使用CXF开发WebService程序的总结(四):基于bean的客户端和服务端代码的编写