问题一:
使用CXF实现WebService,并在客户端实现动态调用编写服务器注意事项
注意 :不要指定
@SOAPBinding(style=Style.RPC, use=Use.LITERAL)
因为cxf 不支持:rpc、encoded,在动态客户调用过程。
问题二:
Caused by: javax.xml.bind.UnmarshalException
这种xml格式化标签的异常, 因此传递带有html标签的html格式数据的时候, 可能会遇到这种问题, ,将html标签格式的数据转换成字符串, 然后传递给webservice, 然后在webservice端再进行解码。
BASE64Encoder encoder = new BASE64Encoder();
String content = encoder.encode(str.getBytes("UTF-8"));
BASE64Decoder decoder = new BASE64Decoder();
new String(decoder.decodeBuffer(content), "UTF-8")
这样就解决了webservice接收 html标签格式数据的问题.
问题三:动态调用响应缓慢,CXF发送、接收消息超时设置
java中设置
//设置超时单位为毫秒 默认是30000毫秒,即30秒。
HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(3000); //连接超时 默认是60000毫秒,即60秒.
httpClientPolicy.setAllowChunking(false); //取消块编码
httpClientPolicy.setReceiveTimeout(3000); //响应超时
http.setClient(httpClientPolicy);
spring 中配置设置
spring+cxf配置方式:
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:jee="http://www.springframework.org/schema/jee"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd ">
<http-conf:conduit name="{WSDL Namespace}portName.http-conduit">
<http-conf:client ConnectionTimeout="10000" ReceiveTimeout="20000"/>
</http-conf:conduit>
</beans>
问题四:传递基本属性
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://127.0.0.1:9000/hello?wsdl");
Object[] objects=client.invoke("sayHello", "张三");
Object[] objects1=client.invoke("sayHello2", "李四");
System.out.println(objects[0].toString()+" "+objects1[0].toString());
问题五:传递对象
方法一:已知实体类所在的包及类名及各个属性
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8080/OrderProcess?wsdl");
//已知service类所在的包server.Order 创建实体类
Object order = Thread.currentThread().getContextClassLoader().loadClass("server.Order").newInstance();
Method m1 = order.getClass().getMethod("setCustomerID", String.class);
Method m2 = order.getClass().getMethod("setItemID", String.class);
Method m3 = order.getClass().getMethod("setQty", Integer.class);
Method m4 = order.getClass().getMethod("setPrice", Double.class);
m1.invoke(order, "C001");
m2.invoke(order, "I001");
m3.invoke(order, 100);
m4.invoke(order, 200.00);
Object[] response = client.invoke("processOrder", order);
System.out.println("Response is " + response[0]);
方法二:已知service实现类所在的包以及对象属性
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8080/OrderProcess?wsdl");
Endpoint endpoint = client.getEndpoint();
// Make use of CXF service model to introspect the existing WSDL
ServiceInfo serviceInfo = endpoint.getService().getServiceInfos().get(0);
QName bindingName = new QName("http://server/", "OrderProcessServiceSoapBinding");
BindingInfo binding = serviceInfo.getBinding(bindingName);
//绑定方法
QName methodName = new QName("http://server/", "processOrder");
BindingOperationInfo boi = binding.getOperation(methodName); // Operation name is processOrder
BindingMessageInfo inputMessageInfo = null;
if (!boi.isUnwrapped()) {
//OrderProcess uses document literal wrapped style.
inputMessageInfo = boi.getWrappedOperation().getInput();
} else {
inputMessageInfo = boi.getUnwrappedOperation().getInput();
}
List<MessagePartInfo> parts = inputMessageInfo.getMessageParts();
//得到第一个参数,以下是对第一个参数的操作。此处只有一个参数
MessagePartInfo partInfo = parts.get(0); // Input class is Order
// Get the input class Order
Class<?> orderClass = partInfo.getTypeClass();
Object orderObject = orderClass.newInstance();
// Populate the Order bean
// Set customer ID, item ID, price and quantity
PropertyDescriptor custProperty = new PropertyDescriptor("customerID", orderClass);
custProperty.getWriteMethod().invoke(orderObject, "C001");
PropertyDescriptor itemProperty = new PropertyDescriptor("itemID", orderClass);
itemProperty.getWriteMethod().invoke(orderObject, "I001");
PropertyDescriptor priceProperty = new PropertyDescriptor("price", orderClass);
priceProperty.getWriteMethod().invoke(orderObject, Double.valueOf(100.00));
PropertyDescriptor qtyProperty = new PropertyDescriptor("qty", orderClass);
qtyProperty.getWriteMethod().invoke(orderObject, Integer.valueOf(20));
// Invoke the processOrder() method and print the result
// The response class is String
Object[] result = client.invoke(methodName, orderObject);
System.out.println("The order ID is " + result[0]);