一、服务端:
1、创建接口:
package cn.tdtk.ws.dao;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import cn.tdtk.ws.domain.Cat;
import cn.tdtk.ws.domain.Horse;
import cn.tdtk.ws.domain.User;
import cn.tdtk.ws.util.FkXmlAdapter;
/** @WebService 所修饰的接口,那么接口里面的方法全部都属于web的服务 */
@WebService
public interface HelloWorld {
//基本数据类型都可以CXF进行实习
String sayHello(String name);
//复杂数据类型
List<Cat> getCatsByUser(User user);
/** CXF不能处理Map<String,Cat>类型,于是采用FkXmlAdapter进行处理。*/
@XmlJavaTypeAdapter(FkXmlAdapter.class)Map<String,Cat> getAllCats();
Horse selectMaxLongName(Horse h1,Horse h2);
}
2、实现接口:
package cn.tdtk.ws.dao.impl;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import cn.tdtk.ws.dao.HelloWorld;
import cn.tdtk.ws.dao.UserService;
import cn.tdtk.ws.domain.Cat;
import cn.tdtk.ws.domain.Horse;
import cn.tdtk.ws.domain.User;
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHello(String name) {
return name + ",你好,现在的时间是: "+new Date();
}
@Override
public List<Cat> getCatsByUser(User user) {
// 在这里只是调用业务逻辑组件,而不实现,具体的实现由业务类去完成
UserService us = new UserServiceImpl();
return us.getCatsByUser(user);
}
@Override
public Map<String, Cat> getAllCats() {
UserService us = new UserServiceImpl();
return us.getAllCats();
}
@Override
public Horse selectMaxLongName(Horse h1, Horse h2) {
if(h1.getName().length()>h2.getName().length())
return h1;
else{
h2.setImage(new DataHandler(new FileDataSource(
new File("c:\\"+File.separator+"1.jpg"))));
return h2;
}
}
}
3、将Map类型与简单类型互相转换:
package cn.tdtk.ws.util;
import java.util.List;
import cn.tdtk.ws.domain.Cat;
public class StringCat {
public static class Entry {
private String key;
private Cat value;
public Entry() {
super();
}
public Entry(String key, Cat value) {
super();
this.key = key;
this.value = 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> entrys;
public List<Entry> getEntrys() {
return entrys;
}
public void setEntrys(List<Entry> entrys) {
this.entrys = entrys;
}
}
4、XmlAdapter转换器:
package cn.tdtk.ws.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import cn.tdtk.ws.domain.Cat;
import cn.tdtk.ws.util.StringCat.Entry;
/**
* 该转换器负责 StringCat 和 Map<String, Cat> 类型的转换
* @author Administrator
*
*/
public class FkXmlAdapter extends XmlAdapter<StringCat, Map<String, Cat>> {
@Override
public Map<String, Cat> unmarshal(StringCat v) throws Exception {
Map<String, Cat> result = new HashMap<String, Cat>();
List<Entry> entrys = v.getEntrys();
for(Entry en : entrys)
result.put(en.getKey(), en.getValue());
return result;
}
@Override
public StringCat marshal(Map<String, Cat> v) throws Exception {
StringCat sc = new StringCat();
List<Entry> entrys = new ArrayList<Entry>();
for(String key : v.keySet())
entrys.add(new Entry(key,v.get(key)));
sc.setEntrys(entrys);
return sc;
}
}
5、业务数据:
package cn.tdtk.ws.dao.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.tdtk.ws.dao.UserService;
import cn.tdtk.ws.domain.Cat;
import cn.tdtk.ws.domain.User;
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,"机器猫","橙色"));
catlist1.add(new Cat(2,"huanhuan","黑色"));
catlist1.add(new Cat(3,"xixi","红色"));
catDb.put(new User(1,"tom","123","shanghai"), catlist1);
List<Cat> catlist2= new ArrayList<Cat>();
catlist2.add(new Cat(1,"lulu","橙色"));
catlist2.add(new Cat(2,"haha","黑色"));
catDb.put(new User(1,"lili","123","shanghai"), catlist2);
}
@Override
public List<Cat> getCatsByUser(User user) {
return catDb.get(user);
}
@Override
public Map<String, Cat> getAllCats() {
Map<String, Cat> maps = new HashMap<String, Cat>();
int i=0;
for(List<Cat> lists : catDb.values()){
for(Cat cat : lists){
i++;
maps.put("第"+i+"个", cat);
}
}
return maps;
}
}