Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

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

$
0
0

这种调用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



 

作者:caihongshijie6 发表于2013-6-2 21:01:03 原文链接
阅读:27 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>