基于spring的调用web形式的cxf服务的方法

这种调用cxf的webservice的形式会导致client增加一些jar包(其实,就是cxf的基本jar包,共67个)

1、新建一个名为CXF_1_client的java project

2、导包

     新建一个lib目录, 将cxf的67个基本jar包放进lib目录中并将其添加到build path

3、在cmd中使用wsdl2java -d . http://localhost:8080/CXF_3/cxf/hi?wsdl命令产生客户端代码,并只保留接口(如IHiService)

4、将3、中产生的接口导进CXF_1_clien项目中(注意,不要改变包结构),代码如下所示:

package com.njupt.cxf;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

/**
 * This class was generated by Apache CXF 2.4.2
 * 2013-06-02T20:37:45.668+08:00
 * Generated source version: 2.4.2
 * 
 */
@WebService(targetNamespace = "http://cxf.njupt.com/", name = "IHiService")
@XmlSeeAlso({})//注意,将原本的大括弧里面的东西删掉,因为他所以来的雷已经删掉了
public interface IHiService {

    @WebResult(name = "return", targetNamespace = "")
    @RequestWrapper(localName = "sayHi", targetNamespace = "http://cxf.njupt.com/", className = "com.njupt.cxf.SayHi")
    @WebMethod
    @ResponseWrapper(localName = "sayHiResponse", targetNamespace = "http://cxf.njupt.com/", className = "com.njupt.cxf.SayHiResponse")
    public java.lang.String sayHi(
        @WebParam(name = "arg0", targetNamespace = "")
        java.lang.String arg0
    );
}


5、App

package com.njupt.cxf;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("com/njupt/cxf/ClientBeans.xml");
	    IHiService hs = (IHiService) ac.getBean("hiService");
	    String ret = hs.sayHi("章泽天,是我们java程序员的女神");
	    
	    System.out.println(ret);
	}
}

6、ClientBeans.xml

在classpath下新建一个名为ClientBeans.xml的file,代码如下:

<?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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
          	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
	<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
	<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" />


    <!--   注意不要写成 <jaxrs:client>  -->
    <jaxws:client id="hiService" serviceClass="com.njupt.cxf.IHiService" address="http://localhost:8080/CXF_3/cxf/hi"></jaxws:client>
    
</beans>	


注意!!如果把<jaxws:client>写成<jaxrs:client>汇报以下错误

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.apache.cxf.jaxrs.client.Client org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean.create()] threw exception; nested exception is javax.ws.rs.WebApplicationException
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:157)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:570)
	... 14 more
Caused by: javax.ws.rs.WebApplicationException
	at org.apache.cxf.jaxrs.AbstractJAXRSFactoryBean.checkResources(AbstractJAXRSFactoryBean.java:312)
	at org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean.createWithValues(JAXRSClientFactoryBean.java:240)
	at org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean.create(JAXRSClientFactoryBean.java:228)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:145)
	... 15 more



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值