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

2023-04-12

这种调用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,代码如下:




	
	
	
	


    
    
    
	


注意!!如果把写成汇报以下错误


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



本文仅代表作者观点,版权归原创者所有,如需转载请在文中注明来源及作者名字。

免责声明:本文系转载编辑文章,仅作分享之用。如分享内容、图片侵犯到您的版权或非授权发布,请及时与我们联系进行审核处理或删除,您可以发送材料至邮箱:service@tojoy.com