您的位置:首页 > 其它

CXF动态客户端调用webservice实例 遇到的问题

2014-12-09 16:43 477 查看
使用CXF实现WebService,并在客户端实现动态调用编写服务器注意事项

注意 :不要指定

@SOAPBinding(style=Style.RPC, use=Use.LITERAL) 因为cxf 不支持:rpc、encoded,在动态客户调用过程。

cxf webservice开发资料,网上一搜大部分是类同的,跟官方的例子一样。

都是简单的静态调用例子。对动态调用的资料以及错误很少。本人被折腾了不行的情况下,

在一哥们的博客 http://hi.baidu.com/flierssp/item/2de0745c1afc1f3b32e0a96f 中看到动态调用不支持RPC才恍然大悟。

服务器端:

package com.rodjson.cxf;

import javax.jws.WebMethod;

import javax.jws.WebParam;

import javax.jws.WebService;

@WebService

/*@SOAPBinding(style=Style.RPC, use=Use.LITERAL) 注意 :如果要动态调用一定要注释掉这句代码*/

public interface HelloWorld {

@WebMethod

String sayHi(@WebParam(name="text") String text);

}

package com.rodjson.cxf;

---HelloWorldImpl---

import javax.jws.WebService;

@WebService(endpointInterface="com.rodjson.cxf.HelloWorld",

serviceName="HelloWorld" )

public class HelloWorldImpl implements HelloWorld {

public String sayHi(String text) {

System.out.println("sayHi called");

return "Hello " + text;

}

}

部署:

package com.rodjson.cxf;

import javax.xml.ws.Endpoint;

public class webServiceApp {

public static void main(String[] args) {

System.out.println("web service start");

HelloWorld implementor = new HelloWorldImpl();

String address = "http://localhost:8080/helloWorld";

Endpoint.publish(address, implementor);

System.out.println("web service end");

}

}

至于部署的方法网上很多,我就不提了,重点是动态客户端调用以及错误的识别

package com.rodjson.cxf;

import org.apache.cxf.endpoint.Client;

import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class ClientObj {

public static void main(String[] args) {

JaxWsDynamicClientFactory factory =JaxWsDynamicClientFactory.newInstance();

Client client =factory.createClient("http://localhost:8080/cxf/services/HelloWorld?wsdl");

try {

Object[] obj =client.invoke("sayHi","xiao");

System.out.println("resp:"+obj[0]);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

错误如下:

2012-5-23 20:53:34 org.apache.cxf.common.jaxb.JAXBUtils logGeneratedClassNames

信息: Created classes:

javacTask: 无源文件

用法: javacTask <options> <source files>

-help 用于列出可能的选项

2012-5-23 20:53:34 org.apache.cxf.endpoint.dynamic.DynamicClientFactory createClient

严重: Could not compile java files for http://127.0.0.1:9082/CrmWeb/services/CrmInterServer?wsdl.

后来我又遇到了org.apache.cxf.interceptor.Fault: Unmarshalling Error: unexpected element (uri:"", local:"p")

这种xml格式化标签的异常, 因此传递带有html标签的html格式数据的时候, 可能会遇到这种问题, 那么就干脆用

BASE64Encoder encoder = new BASE64Encoder();

String content = encoder.encode(str.getBytes("UTF-8"));

传输数据, 将html标签格式的数据转换成字符串, 然后传递给webservice, 然后在webservice端再进行解码

BASE64Decoder decoder = new BASE64Decoder();

new String(decoder.decodeBuffer(content), "UTF-8")

这样就解决了webservice接收 html标签格式数据的问题.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: