一般如何解决这种Map以及非JavaBean的复杂数据类型
(1)在接口中返回这个数据类型需要使用 @XmlJavaTypeAdapter(value=**类)
(2)自己实现这个**类,这个类需要继承XmlAdapter类,这个类的功能就是将不能处理的类转换成CXF框架能够处理的类型。
例如以下一个例子就是解决Web Service 服务传输Map类型的例子。
Web Service 的Server端
第一步创建一个接口:
package yy;
import java.util.List;
@WebService
public interface HelloWorld {
public @XmlJavaTypeAdapter(value = FKXmlAdapter.class) Map<String,Cat> getAllCats();
}
第二部创建一个实现类:
package yy.impl;import java.util.Date;
@WebService(endpointInterface="yy.HelloWorld",serviceName="HelloWorldWS")public class HelloWorldWS implements HelloWorld{
@Override
public Map<String, Cat> getAllCats() {
UserService service=new UserServiceImpl();
return service.getAllCats();
}
}
package service;
import java.util.List;
public interface UserService {
public Map<String,Cat> getAllCats();
}
package service.impl;
import java.util.ArrayList;
public class UserServiceImpl implements UserService{
static Map<User,List<Cat>> catDb=new HashMap<User,List<Cat>>();
static{
List<Cat> catList1=new ArrayList<Cat>();
catList1.add(new Cat(1,"garefkds","blcak"));
catList1.add(new Cat(2,"fds","bldascak"));
catDb.put(new User(1,"sun","2233","dad"), catList1);
List<Cat> catList2=new ArrayList<Cat>();
catList2.add(new Cat(3,"garefkds","blcak"));
catList2.add(new Cat(4,"fds","bldascak"));
catDb.put(new User(2,"sun","2233","dad"), catList2);
}
@Override
public Map<String, Cat> getAllCats() {
Map<String,Cat> result=new HashMap<String,Cat>();
int i=1;
for(List<Cat> cats:catDb.values()){
for(Cat cat:cats){
result.put(""+i++, cat);
}
}
return result;
}
}
package yy.domain;
public class Cat {
private Integer id;
private String name;
private String color;
public Cat() {
}
public Cat(Integer id, String name, String color) {
super();
this.id = id;
this.name = name;
this.color = color;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
第三部就是创建一个类别转换器
package yy.util;
import javax.xml.bind.annotation.adapters.XmlAdapter;
//StringCat 类型是我们自定义的类型,CXF可以处理的类型,Map《String,Cat》是CXF无法处理的类型
//这个类的功能就是StringCat类与Map<String,Cat>类之间进行转换
public class FKXmlAdapter extends XmlAdapter<StringCat,Map<String,Cat>>{
@Override
public StringCat marshal(Map<String, Cat> arg0) throws Exception {
StringCat cat=new StringCat();
Set<String> keys=arg0.keySet();
List<Entry> list=new ArrayList<Entry>();
for(String key:keys){
Entry entry=new Entry();
entry.setKey(key);
entry.setValue(arg0.get(key));
list.add(entry);
}
cat.setEntries(list);
return cat;
}
@Override
public Map<String, Cat> unmarshal(StringCat arg0) throws Exception {
Map<String, Cat> result=new HashMap<String,Cat>();
for(Entry entry:arg0.getEntries()){
result.put(entry.getKey(), entry.getValue());
}
return result;
}
}
package yy.util;
import java.util.List;
public class StringCat {
public static class Entry{
private String key;
private Cat value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Cat getValue() {
return value;
}
public void setValue(Cat value) {
this.value = value;
}
}
private List<Entry> entries;
public List<Entry> getEntries() {
return entries;
}
public void setEntries(List<Entry> entries) {
this.entries = entries;
}
}
第5步就是发布WebService程序:
package lee;
import javax.xml.ws.Endpoint;
public class ServerMain {
public static void main(String[] args){
HelloWorld hello=new HelloWorldWS();
//发布WebService
Endpoint.publish("http:/IP/webservie" ,hello);
}
}