大致思路指引:
首先cxf是不能直接传递map类型,但是可以通过我们自己的处理来把map转换为cxf可以处理的类型,进行传递。
注意在服务端需要在原有的基础上增添一个Jar包:xstream-1.1.3.jar
这里重新把jar截图:
.在服务接口中添加
@XmlJavaTypeAdapter(MapAdapter.class)Map<String,Cat> getAllCats();
对这个接口中的@XmlJavaTypeAdapter是Java提供的一个转化注解,它通过执行后便括号中的类MapAdapter中的方法进行类型的转化,MapAdapter这个类需要我们自己写代码如下,
package util;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.fkjava.cxf.entity.Cat;
import org.fkjava.cxf.entity.StringCat;
import org.fkjava.cxf.entity.StringCat.Entry;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
//StringCat是cxf可以处理的类型,map为转换类型
public class MapAdapter extends XmlAdapter<StringCat, Map<String,Cat>> {
/**
* 把Map<String, Cat>转换成StringCat
*/
@Override
public StringCat marshal(Map<String, Cat> v) throws Exception {
StringCat sc = new StringCat();
for(String s:v.keySet()){
sc.getEntries().add(new Entry(s,v.get(s)));
}
return sc;
}
/**
* 把StringCat转换成Map<String, Cat>
*/
@Override
public Map<String, Cat> unmarshal(StringCat v) throws Exception {
Map<String,Cat> result = new HashMap<String,Cat>();
for(Entry entry :v.getEntries()){
result.put(entry.getKey(), entry.getValue());
}
return result;
}
}
在这里我们定义StringCat类型:
package org.fkjava.cxf.entity;
import java.util.ArrayList;
import java.util.List;
public class StringCat {
private List<Entry> entries=new ArrayList<Entry>();
public List<Entry> getEntries() {
return entries;
}
public void setEntries(List<Entry> entries) {
this.entries = entries;
}
public static class Entry{
private String key;
private Cat value;
public Entry() {
}
public Entry(String key, Cat cat) {
this.key = key;
this.value = cat;
}
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;
}
}
}
userService接口的代码以及实现类的代码:用来模拟假数据返回一个map类型
package org.fkjava.cxf.service;
import java.util.List;
import java.util.Map;
import org.fkjava.cxf.entity.Cat;
import org.fkjava.cxf.entity.User;
public interface UserService {
Map<String, Cat> getAllCats();
}
package org.fkjava.cxf.ws.impl;
import java.awt.CardLayout;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.fkjava.cxf.entity.Cat;
import org.fkjava.cxf.entity.User;
import org.fkjava.cxf.service.UserService;
public class UserServiceImpl implements UserService {
//用一个hashmap来模拟数据库
static Map<User, List<Cat>> catDb=new HashMap();
static {
List<Cat> catList1=new ArrayList<Cat>();
catList1.add(new Cat(1,"garfield1","zhengse1"));
catList1.add(new Cat(2,"机器猫1","白色1"));
catDb.put(new User(1,"周杰伦1","男1",40,"台湾1","pass1"), catList1);
List<Cat> catList2=new ArrayList<Cat>();
catList2.add(new Cat(3,"garfield2","zhengse2"));
catList2.add(new Cat(4,"机器猫2","白色2"));
catDb.put(new User(2,"周杰伦2","男2",40,"台湾2","pass2"), catList2);
List<Cat> catList3=new ArrayList<Cat>();
catList3.add(new Cat(5,"garfield3","zhengse3"));
catList3.add(new Cat(6,"机器猫3","白色3"));
catDb.put(new User(3,"周杰伦3","男",40,"台湾3","pass3"), catList3);
List<Cat> catList4=new ArrayList<Cat>();
catList4.add(new Cat(7,"garfield4","zhengse4"));
catList4.add(new Cat(8,"机器猫4","白色4"));
catDb.put(new User(4,"周杰伦4","男4",40,"台湾4","pass4"), catList4);
List<Cat> catList5=new ArrayList<Cat>();
catList5.add(new Cat(9,"garfield5","zhengse5"));
catList5.add(new Cat(10,"机器猫5","白色5"));
catDb.put(new User(5,"周杰伦5","男5",40,"台湾","pass5"), catList5);
}
@Override
public Map<String, Cat> getAllCats() {
HashMap<String, Cat> result=new HashMap<String, Cat>();
int i=0;
for(List<Cat> cats:catDb.values()){
for(Cat cat:cats){
i++;
result.put(i+"", cat);
}
}
return result;
}
}
服务端暴露服务的代码
package org.fkjava.cxf.ws.impl;
import java.util.*;
import javax.jws.WebService;
import org.fkjava.cxf.entity.Cat;
import org.fkjava.cxf.entity.User;
import org.fkjava.cxf.service.UserService;
import org.fkjava.cxf.ws.HelloWord;
@WebService(endpointInterface="org.fkjava.cxf.ws.HelloWord",serviceName="HelloWorldImpl")
public class HelloWorldImpl implements HelloWord {
@Override
public Map<String, Cat> getAllCats() {
UserService us=new UserServiceImpl();
return us.getAllCats();
}
}
在cmd下执行命令:wsdl2java
客户端的显示
package lee;
import java.util.List;
import org.fkjava.cxf.ws.Cat;
import org.fkjava.cxf.ws.Entry;
import org.fkjava.cxf.ws.HelloWord;
import org.fkjava.cxf.ws.StringCat;
import org.fkjava.cxf.ws.User;
import org.fkjava.cxf.ws.impl.HelloWorldImpl;
public class ClientMain {
public static void main(String[] args) {
HelloWorldImpl factory=new HelloWorldImpl();
//此处返回的只是远程调用web service的代理,远程服务端的服务器不能关闭
HelloWord hw = factory.getHelloWorldImplPort();
StringCat allCats = hw.getAllCats();
List<Entry> list = allCats.getEntries();
for (Entry entry : list) {
System.out.println(entry.getKey()+"----"+entry.getValue().getName());
}
}
}
未完待续-----------