当前位置: 代码迷 >> Web前端 >> 应用CXF开发WebService的笔记_客户端
  详细解决方案

应用CXF开发WebService的笔记_客户端

热度:439   发布时间:2012-10-08 19:54:56.0
使用CXF开发WebService的笔记_客户端

2.3 书写客户器端的服务和配置CXF

?? 新建一java工程,导入cxf66个文件和ws.userinfo.jar,然后书写配置文件,也在src下新建一个applicationContext_cxf.xml,具体代码是

<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:soap="http://cxf.apache.org/bindings/soap"

??? xsi:schemaLocation="

?????? http://www.springframework.org/schema/beans

?????? http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

?????? http://cxf.apache.org/bindings/soap

?????? http://cxf.apache.org/schemas/configuration/soap.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" />

?

??? <jaxws:client id="userWebService"

?????? address="http://127.0.0.1:8089/webproject/services/callUserService"

??????? //这个地址必须和wsdl写的地址一致哦

?????? serviceClass="com.demo.service.userinfo.IUserService">

??????? //这里必须是服务的接口类

??? </jaxws:client>

</beans>

?

有了这个配置文件,就很好写测试类,代码如下:

package com.peter.userinfo.test;

?

import com.demo.service.userinfo.IUserService;

?

public class AppWsClient {

???

??? public static void main(String args[]){

?????? IUserService userInfo = (IUserService) ApplicationContextUtils

?????? .getApplicationContext().getBean("userWebService");

?????? System.out.println(" client has been initiated ...... ");

?????? String password=userInfo.getPassword("admin");

?????? System.out.println("returnStr = "+password);

??? }

}

?

帮助类很好理解(这个是做demo用的):

package com.peter.userinfo.test;

?

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

?

public class ApplicationContextUtils {

??? private static ApplicationContext applicationContext;

??? private static final String[] locations = {

?????????? "applicationContext_cxf.xml" };

??? static {

?????? if (applicationContext == null)

?????????? applicationContext = rebuildApplicationContext();

??? }

?

??? public static ApplicationContext rebuildApplicationContext() {

?????? return new ClassPathXmlApplicationContext(locations);

??? }

?

??? public static ApplicationContext getApplicationContext() {

?????? return applicationContext;

??? }

?

?

??? public static void main(String[] args) {

?????? rebuildApplicationContext();

?????? if (applicationContext == null) {

?????????? System.out.println("ApplicationContext is null");

?????? } else {

?????????? System.out.println("ApplicationContext is not null!");

?????? }

??? }

}

现在可以把服务端先启起来,然后运行客户端的程序,就能看到如下结果:

客户端的打印结果:

client has been initiated ......

returnStr = admin's password=123456

服务器端的打印是

Server's UserInfoImpl's getPassord() is called, and loginName=admin

和以前的AXIX2XfireWebService接口的开发,CXF确实简单多,开发量少了,而且比较容易理解。

  相关解决方案