标题:Apache CXF学习笔记二-复杂数据类型
作者:kagula
最后更新日期:2016-09-06
环境:
[1]JDK 1.7.x
[2]Eclipse Mars.2 J2EE Version
[3]Spring 3.1.2.RELEASE
[4]CXF 3.0.9
概述:
用户自定义class引用自定义class是被CXF直接支持的,
重命名参数节点名称也做了示例 这在学习笔记一里已经测试过了。
现在要测试
[1]修改自定义class成员的xml节点名(笔记一中只测试了指定root node的名)
[2]list容器。
等复杂参数的传递。
正文
服务端
pom.xml参考《Apach CXF学习笔记一》。
Step1:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>WSTest2</display-name>
<!-- CXF -->
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</web-app>
Step2:要传递的数据类型
例一用的数据类型二个
package com.kagula.datatype;
public class DataTypeMyPackage {
//对象名称会被作为xml节点名称
public DataTypeContainNameMapping dtcnm;
public DataTypeMyPackage()
{
dtcnm = new DataTypeContainNameMapping();
}
}
package com.kagula.datatype;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
/*
* 这里示范重命名节点名称
*/
//@XmlType也可以指定生成字段顺序,例如
//@XmlType(propOrder = { "author", "name", "publisher", "isbn" })
public class DataTypeContainNameMapping {
//对象名称会被作为xml节点名称
public int id;
//把node的节点名称重定义为”mymessage“
@XmlElement(name="mymessage")
//因为上面的annotation只能调用一次,所以这次使用了“field”,而不是学习笔记一中的“attribute”。
//因为"attribute"有get/set对,导致两次相同的(隐式)annotation。
public String message;
}
例二用的数据类型三个
package com.kagula.datatype;
public class DataTypeP {
public String p;
}
package com.kagula.datatype;
import java.util.List;
public class DataTypeA extends DataTypeP{
public String a;
public List<DataTypeB> listB;
}
package com.kagula.datatype;
public class DataTypeB {
public String b;
public DataTypeB() {}
public DataTypeB(String s) {
b = s;
}
}
Step3:定义WS接口
package com.kagula;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import com.kagula.datatype.DataTypeA;
import com.kagula.datatype.DataTypeMyPackage;
@WebService
public interface IService {
//@WebParam指定dtmp of DataTypeMyPackage的节点名称为package,如果不指定节点名为“arg0”。
//@WebResult指定返回节点名称为package,如果不指定节点名为“return”。
@WebResult(name = "package") DataTypeMyPackage sayHi(@WebParam(name="package") DataTypeMyPackage dtmp);
//这次测带inherit和list特性的对象传递
DataTypeA sayHi2(DataTypeA aaa);
}
Step4:实现WS接口
package com.kagula;
import com.kagula.datatype.DataTypeA;
import com.kagula.datatype.DataTypeB;
import com.kagula.datatype.DataTypeMyPackage;
public class ServiceImpl implements IService{
@Override
public DataTypeMyPackage sayHi(DataTypeMyPackage dtmp)
{
dtmp.dtcnm.id += 1;
dtmp.dtcnm.message = ("i got ["+dtmp.dtcnm.message+"] message.");
return dtmp;
}
@Override
public DataTypeA sayHi2(DataTypeA aaa)
{
aaa.p = "i got " + aaa.p;
aaa.a = "i got " + aaa.a;
for(int i=0;i<aaa.listB.size();i++)
{
aaa.listB.set(i,
new DataTypeB(aaa.listB.get(i).b + "..."));
}
return aaa;
}
}
Step5::cxf-servlet.xml
和web.xml放在同一个目录中
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<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:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:server id="jaxwsService" serviceClass="com.kagula.IService" address="/hello">
<jaxws:serviceBean>
<bean class="com.kagula.ServiceImpl"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
客户端
是一个Java Application可以直接运行
源代码清单如下
package com.kagula;
import java.util.ArrayList;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import com.kagula.datatype.DataTypeA;
import com.kagula.datatype.DataTypeB;
import com.kagula.datatype.DataTypeMyPackage;
public class Client {
/*
* QName的第一个参数是namespace可以在
* http://localhost:8080/WSServerTest/services/hello?wsdl
* xmlns:tns属性中查看
* 或服务端源码package的名称倒一下。
* 第二个参数,是提供WS服务的接口名
* PORT_NAME的第二个参数,是提供WS服务的接口名+"Port"
*/
private static final QName SERVICE_NAME = new QName("http://kagula.com/", "IService");
private static final QName PORT_NAME = new QName("http://kagula.com/", "IServicePort");
public static void main(String[] args) throws Exception
{
Service service = Service.create(SERVICE_NAME);
String endpointAddress = "http://localhost:8080/WSTest2/ws/hello";
// Add a port to the Service
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
//
IService is = service.getPort(IService.class);
//添加CXF自带的拦截器后,可以在Console中查看发送和返回的soap信息
org.apache.cxf.endpoint.Client client = ClientProxy.getClient(is);
client.getInInterceptors().add(new LoggingInInterceptor());
client.getOutInterceptors().add(new LoggingOutInterceptor());
//测试重命名节点
DataTypeMyPackage dtmp = new DataTypeMyPackage();
dtmp.dtcnm.id = 1;
dtmp.dtcnm.message=("chocolate and pie");
System.out.println(is.sayHi(dtmp).dtcnm.message);
System.out.println("==============================================");
//测试带继承和list的对象
DataTypeA aaa = new DataTypeA();
aaa.a = "aaa";
aaa.p = "ppp";
aaa.listB = new ArrayList<DataTypeB>();
aaa.listB.add(new DataTypeB("bb0"));aaa.listB.add(new DataTypeB("bb1"));
DataTypeA dtaResult = is.sayHi2(aaa);
System.out.println(dtaResult.p+","+dtaResult.a);
for(int i=0;i<dtaResult.listB.size();i++)
{
System.out.println(dtaResult.listB.get(i).b);
}
}//end function
}//end class
备注
[1]在“http://mvnrepository.com/artifact”下可以查询依赖库是否存在。
[2]CXF3.0.9的示例代码可以通过下面的地址下载
http://www.apache.org/dyn/closer.lua/cxf/3.0.9/apache-cxf-3.0.9.zip
常见问题
Q Cannot change version of project facet Dynamic Web Module to 2.5
A
Step1:修改项目.settings路径下的org.eclipse.wst.common.project.facet.core.xml文件
jdk设成1.7,jst.web设成2.5,web.xml默认的2.3也要改成2.5。
根据参考资料[3]我的tomcat是7应该设成3,但是这里只是做个CXF测试,所以随便设了个2.5版本。
Q 类成员没有映射到xml节点中
A 还没有为类成员分配对象。
参考资料
[1]《Customising class name with @XmlType annotation》
https://www.ibm.com/developerworks/community/blogs/738b7897-cd38-4f24-9f05-48dd69116837/entry/customising_class_name_with_xmltype_annotation3?lang=en
[2]《@XmlElement and useless 'required' parameter》
使不存在的对象也输出个xml节点。
http://stackoverflow.com/questions/19266391/xmlelement-and-useless-required-parameter
[3]《Annotation Type XmlSeeAlso》
http://docs.oracle.com/javaee/7/api/javax/xml/bind/annotation/XmlSeeAlso.html
补充资料
[1]《Spring mvc 和 CXF 搭建SOAP环境》
http://my.oschina.net/long0419/blog/192788
[2]《Making Spring MVC and CXF play well together》
https://ayax79.wordpress.com/2009/02/19/making-spring-mvc-and-cxf-play-well-together/#comment-110
[3]《使用CXF为Web Service添加拦截器,自定义拦截器》
http://blog.csdn.net/a9529lty/article/details/8437599
[4]《CXF对java type与xml其间的marshall,unmarshall》
http://www.myexception.cn/xml-soap/668610.html
[5]《CXF+JAXB处理复杂数据》
http://www.cnblogs.com/holbrook/archive/2012/12/15/2818833.html#sec-3-3
[6]《Change CXF Character Encoding》
http://www.javatips.net/blog/change-cxf-character-encoding
[7]《Change the response character set on CXF web service》
http://stackoverflow.com/questions/15528709/change-the-response-character-set-on-cxf-web-service