基于spring的调用web形式的cxf服务的方法
2013-06-02 21:01
573 查看
这种调用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
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
相关文章推荐
- 使用cxf框架搭建基于Spring远程调用Web服务
- 服务调用RMI远程方法调用示例【基于Spring配置文件】
- Spring boot构建基于rest的Web服务
- spring boot整合cxf发布webservice服务和cxf客户端调用
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务的方法(推荐)
- 1 使用 CXF 和 Spring 创建 Web 服务
- Java WebService-CXF-基于SOAP的Web服务
- jQuery AJAX实现调用页面后台方法和web服务定义的方法分享
- C#动态调用Web服务方法
- post方法调用web服务(改进)
- Web应用中使用CXF不基于Spring的WebService发布
- WebService-CXF-Spring基于web的cxf
- java调用百度地图web服务api-----该方法可用在js跨域请求上
- 开发自己的Web服务处理程序(以支持Ajax框架异步调用Web服务方法)
- springboot整合cxf发布webservice以及调用的方法
- CXF webservice之手动启动服务方法(restful )
- Webservice的cxf开发_使用spring发布服务+ajax调用服务
- 从webconfig中动态调用web服务,找到的一个简单方法!!!!
- WebService-CXF-Spring 基于web的cxf(三)------参考传智播客视频
- WEB服务动态解析及调用技术的实现(基于WSDL4J及SAAJ)