`
awtqty_zhang
  • 浏览: 90477 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

CXF2.5+Spring3.0搭建Web Services应用程序三------生成客户端代码

    博客分类:
  • CXF
 
阅读更多

一、生成客户端代码:

当编写完成服务器端代码后,则可通过命令自动生成客户端代码。

 

wsdl2java -p ws.client -d E:\eclipse_workspace\src -verbose http://localhost:8080/TestService?wsdl

 

 各参数详解见http://cwiki.apache.org/CXF20DOC/wsdl-to-java.html 

注:在使用该命令生成的客户端代码时,代码可能会报错,这是由于CXF中与原项目中存在相同的名称的jar包,解决此问题有三种方法:

1、将项目中的CXF的包提到项目最前端。

2、将报错代码删除(一般不影响正常使用)。

3、使用降低版本规约的方法生成客户端代码,命令如下:

 

wsdl2java -frontend jaxws21 -p ws.client -d E:\eclipse_workspace\src -verbose http://localhost:8080/TestService?wsdl 

 

 说明:该命令生成的客户端代码与使用第二种处理方法的代码完全一致。

二、测试:

到这里一个简单的Web Services应用程序就已经搭建完成了,现在就来进行一下测试,看能否正常执行。

1、在测试项目中新建bean.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"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://cxf.apache.org/jaxws 
        http://cxf.apache.org/schemas/jaxws.xsd"
	default-autowire="byName">

	<jaxws:client id="testService" serviceClass="ws.client.ITestService" address="http://my.cogobuy.com:8080/TestService"/>
</beans>

  2、建新测试方法,代码如下:

 

public class TestClent {
    
    /* 客户端service */
    private ITestService testService;
    
    /**
     * @Description
     * @throws java.lang.Exception
     * @return void
     * @throws
     */
    @Before
    public void setUp() throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        testService = (ITestService) context.getBean("testService");
    }
    
    @Test
    public void test01() throws Exception {
        TestUser user = testService.getUserProperty("awbqty_zhang", "123456");
        System.out.println(user.getName() + "  " + user.getEnName() + "  " + user.getPhone() + "         "+ user.getMail() + "  "+ user.getNickName());
    }
}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics