用CXF实现webservice解析xml,怎么接受xml格式的数据

其他回答(6)
直接使用系统生成的不可以吗?
你的这个需求也曾想过,毕竟自动的soap有点复杂,但我从没去实践过。
给你个建议:从attribute着手,应该是一个解决方案。
收获园豆:20
园豆:5762
园豆:5762
确认一下,是请求报文,还是你的WebService返回的报文?
收获园豆:20
园豆:34787
园豆:34787
园豆:34787
没写过自定义格式的,长知识了。
收获园豆:10
园豆:2539
已经解决,解决方法如下。
1.首先使用字符串拼接出来自定义的返回报文格式data。
2.使用如下方法输出
Context.Response.ContentEncoding = System.Text.Encoding.UTF8;Context.Response.ContentType = "text/xml";Context.Response.Write(data);Context.Response.Flush();Context.Response.End();
楼主,请问你这个问题怎么解决的?可以请教你一下吗?谢谢了,我的QQ:麻烦楼主指点一下,非常感谢!
&&&您需要以后才能回答,未注册用户请先。博客分类:
环境:JDK6.0,cxf-2.0.7,spring2.5,tomcat6.0
服务器端:
1.新建Web项目,例如webws,导入cxf-2.0.7/lib下的jar包和spring2.5的包,因为cxf支持spring,因此它自带有sping的一些核心包,为了以后扩展,保险起见都一起导入吧。。
2.新建一个服务接口IHelloWorld.java,该接口是面向客户端,暴露服务:
.
import javax.jws.WebP
import javax.jws.WebS
@WebService
public interface IHelloWorld {
//@WebParam(name="arg0")可有可无,为了增强可读性
public String sayHello(@WebParam(name="arg0")String text);
}
3.再新建该接口的实现类HelloWorldImpl.java:
.
import javax.jws.WebS
@WebService(endpointInterface="cn.com.service.IHelloWorld")
public class HelloWorldImpl implements IHelloWorld {
public String sayHello(String text) {
return "Hello" +
}
4.现在可以进行spring配置了,在eclipse的src文件夹下新建applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"&
&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" /&
&bean id="hello" class="cn.com.service.HelloWorldImpl"/&
&jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /&
&/beans&
注意:1).&beans&元素里面的命名空间一定要正确,特别要增加xmlns:jaxws="http://cxf.apache.org/jaxws",http://cxf.apache.org/jaxws,http://cxf.apache.org/schemas/jaxws.xsd;
2).cxf.xml,cxf-extension-soap.xml,cxf-servlet.xml三个文件都在cxf-2.0.7.jar中把它们拷贝到META-INF/目录下。
5.配置web.xml:
&?xml version="1.0" encoding="UTF-8"?&
&web-app version="2.5" xmlns="/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="/xml/ns/javaee
/xml/ns/javaee/web-app_2_5.xsd"&
&context-param&
&param-name&contextConfigLocation&/param-name&
&param-value&classpath:applicationContext.xml&/param-value&
&/context-param&
&listener&
&listener-class&org.springframework.web.context.ContextLoaderListener&/listener-class&
&/listener&
&servlet-name&CXFServlet&/servlet-name&
&display-name&CXF Servlet&/display-name&
&servlet-class&org.apache.cxf.transport.servlet.CXFServlet&/servlet-class&
&load-on-startup&1&/load-on-startup&
&/servlet&
&servlet-mapping&
&servlet-name&CXFServlet&/servlet-name&
&url-pattern&/*&/url-pattern&
&/servlet-mapping&
&welcome-file-list&
&welcome-file&index.jsp&/welcome-file&
&/welcome-file-list&
&/web-app&
到此服务器端基本上告一段落,可以将应用部署到tomcat,启动并访问http://localhost:8080/webws/HelloWorld?wsdl,如果能正确显示xml文件则说明部署成功。
1.新建一个java项目,例如wsclient,也导入cxf-2.0.7和spring包
2.新建客户程序(如果熟悉cxf可以运用cxf的wsdl2java命令直接根据wsdl生成),如Test.java:
package pro.webws.
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-client.xml");
IHelloWorld client = (IHelloWorld) ctx.getBean("client");
String result = client.sayHello("你好!");
System.out.println(result);
}
根据代码,我们知道还需要新建一个IHelloWorld接口,这是服务器端暴露的服务,我们还需要在客户端再新建一个以利用之,代码和服务器端一致即可,不再赘述。
现在可以配置客户端的spring文件了,新建spring-client.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:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schema/jaxws.xsd"&
&bean id="client" class="pro.webws.client.IHelloWorld" factory-bean="clientFactory"
factory-method="create" /&
&bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"&
&property name="serviceClass" value="pro.webws.client.IHelloWorld" /&
&property name="address" value="http://localhost:8080/webws/HelloWorld" /&
&/beans&
注意:&property name="address" value="http://localhost:8080/webws/HelloWorld" /&中的/HelloWorld要与服务器端applicationContext.xml中的&jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /&的address属性对应。
现在服务器和客户端都已完成,启动tomcat,运行客户程序,将输出结果:
Hello你好!
浏览 54951
tsinglongwu
浏览: 163566 次
来自: 深圳
服务端没有问题 客户端的运行的时候报实例化异常 不能实例化我的 ...
在哪里可以下载demo ?
谢谢!成功。
你好 大神,问下 onUpload$browse(Upload ...
楼主,我用楼主写的实例运行时,在tomcat启动时就报异常了: ...本帖子已过去太久远了,不再提供回复功能。博客分类:
一直以来很少在Iteye上发帖,这次也是闲来无趣。把自己总结的利用JSON格式传递复杂对象的WebService的例子发上来,好让新手学习下。
准备工作
CXF http://cxf.apache.org/
Gson /p/google-gson/
众所周知,WebService传输复杂对象是比较麻烦的事情,因为要实现个可序列化接口。
假设现在一个自定义类型里有List,并且该List里仍旧是自定义类型
这样就比较麻烦了。但是我们可以简单的搞定。前提是利用Gson,传输Json格式的字符串是一个比较高效率的方案,本人不怎么喜欢XML,因为XML格式的字符串增加了报文长度。
我也不多说废话。还是上附件吧。两个工程,Eclipse工程。
附件为三个,其中cxf-2.42.jar本来应当在CXFServer包得lib目录里,但是Iteye附件限制10MB,只好拆出来了。
下载次数: 3035
下载次数: 1470
下载次数: 841
浏览 12414
浏览: 84416 次
来自: 北京
你们知道刘绍华么?就是北邮的一个教授,专门研究WebRTC的资 ...
为什么用tomcat一运行就报错?十二月 01, 2014 3 ...
楼主能写下4.2版本的suggest教程不谢谢
现在火狐也支持...博客分类:
WebService实现
1、 整个项目使用CXF来实现,在实现的过程中,在MyEclipse中对CXF下lib中的所有jar文件通过引入外部包来处理。
2、 在MyEclipse6.5中可以实现服务器端和客户端,但是客户端在使用wsdl进行动态调用的过程中总是报错,最后使用MyEclipse9.0实现了进行动态调用的实现。其中发生的错误如下:
(1)Exception in thread "main" java.lang.LinkageError: 正在从引导类加载器加载 JAXB 2.1 API, 但此 RI (来自jar:file:/D:/CXF/lib/jaxb-impl-2.2.5.jar!/com/sun/xml/bind/v2/model/impl/ModelBuilder.class) 需要 2.2 API。请使用授权目录机制将 jaxb-api.jar 放在引导类加载器中。(请参阅 /j2se/1.6.0/docs/guide/standards/)
解决办法:
通过删除引入包中的jaxb-impl-2.2.5.jar文件可以解决。
(2)java.lang.IllegalArgumentException: Can not set final com.sun.tools.internal.xjc.reader.internalizer.InternalizationLogic field com.sun.tools.internal.xjc.reader.internalizer.DOMForest.logic to org.apache.cxf.endpoint.dynamic.DynamicClientFactory$1
解决办法:
此错误最后还是没有解决,不知道如何解决。
3、 在MyEclipse9.0实现时又发生如下错误:
org.mon.i18n.UncheckedException: No operation was found with the name {/}sayHello.
4、 原因在于@WebService说明中没有使用targetNamespace造成的。
@WebService(endpointInterface="com.cxf.interfaces.IHelloWorldService",serviceName="helloWorldService")
修改如下:
@WebService(endpointInterface="com.cxf.interfaces.IHelloWorldService",serviceName="helloWorldService",targetNamespace="/")
5、 整个程序代码如下:
(1)接口的定义:
package com.cxf.
import javax.jws.WebP
import javax.jws.WebS
@WebService
public interface IHelloWorldService {
&&&
&&& String sayHello(@WebParam(name="username") String username);
&&&
}
(2)服务器端的实现:
package com.cxf.
import javax.jws.WebS
import javax.xml.ws.E
import com.cxf.interfaces.IHelloWorldS
@WebService(endpointInterface="com.cxf.interfaces.IHelloWorldService",serviceName="helloWorldService",targetNamespace="/")
public class Server implements IHelloWorldService{
&&& public String sayHello(String username) {
&&&&&&& return "Hello,"+
&&& }
&&& public static void main(String[] args) {
&&&&&&& Server impl=new Server();
&&&&&&& String address="http://127.0.0.1:9000/hello";
&&&&&&& Endpoint.publish(address, impl);
&&& }
}
(3)客户端的实现:
package com.cxf.
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientF
public class ClientFromWsdl {
&&&
&&& public static void main(String[] args) {
&&&
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
&&&
org.apache.cxf.endpoint.Client client = dcf.createClient("http://127.0.0.1:9000/hello?wsdl");
&&&&&&&&&&& //sayHello 为接口中定义的方法名称&& 张三为传递的参数&& 返回一个Object数组
&&&
Object[] objects=client.invoke("sayHello", "张三");&&&
//输出调用结果
&&&
System.out.println(objects[0].toString());
&&&
catch(Exception e)
&&&
e.printStackTrace();
&&&
}
&&& }
}
(4)客户端的实现(非动态的)
package com.cxf.
import org.apache.cxf.interceptor.LoggingInI
import org.apache.cxf.interceptor.LoggingOutI
import org.apache.cxf.jaxws.JaxWsProxyFactoryB
import com.cxf.interfaces.IHelloWorldS
public class Client {
&&& public static void main(String[] args) {
&&&&&&& JaxWsProxyFactoryBean& factoryBean=new JaxWsProxyFactoryBean();
&&&&&&& factoryBean.getInInterceptors().add(new LoggingInInterceptor());
&&&&&&& factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
&&&&&&& factoryBean.setServiceClass(IHelloWorldService.class);
&&&&&&& factoryBean.setAddress("http://localhost:9000/hello");
&&&&&&& IHelloWorldService impl=(IHelloWorldService) factoryBean.create();
&&&&&&& System.out.println(impl.sayHello("张三"));
&&& }
}
浏览 19372
浏览: 24195 次
来自: 天津
上面的 2 - (2) 我也遇到了,又是 ClassLoade ...

我要回帖

更多关于 webservice xml 的文章

 

随机推荐