web service就是为了不同平调用台之间的调用,假如C写的程序需要由java调用。而不同平台之间调用需要某种媒介,使得能互相了解对方的意思。就像一个人会说中文,但不会说英文;另一个人会说英文,但不会说中文,这两个人要交谈,就需要第三个人,既会说中文,又会说英文来当两者的翻译。而XML就是不同语言之间的翻译,只要一种语言支持XML的生成与解析,那该语言就支持web service。下面简单运用一下。
使用CXF开发web service。
服务器端:
1、要暴露的接口
package com.webservice.cxf;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import com.webservice.cxf.domain.Cat;
import com.webservice.cxf.domain.User;
import com.webservice.cxf.util.XMLAdapter;
@WebService
public interface IHelloWorld
{
//web service处理简单的字符串对象
String sayHi(String name);
//web service处理list集合
List<Cat> getCatsByUser(User user);
//web service处理其不支持的map集合,需要另加一个适配器,使其将不支持的类型转化为支持的类型
@XmlJavaTypeAdapter(XMLAdapter.class) Map<String, Cat> getAllCats();
}
2、实现类:
package com.webservice.cxf.impl;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import com.webservice.cxf.IHelloWorld;
import com.webservice.cxf.domain.Cat;
import com.webservice.cxf.domain.User;
import com.webservice.cxf.service.IUserService;
import com.webservice.cxf.service.impl.UserServiceImpl;
@WebService(endpointInterface = "com.webservice.cxf.IHelloWorld",
serviceName = "HelloWorldImpl")
//该serviceName对应的是客户端的实现接口的类名
public class HelloWorldImpl implements IHelloWorld
{
@Override
public String sayHi(String name)
{
return name + "您好";
}
@Override
public List<Cat> getCatsByUser(User user)
{
//通过另外的service层实现具体的业务逻辑
IUserService userService = new UserServiceImpl();
return userService.getCatsByUser(user);
}
@Override
public Map<String, Cat> getAllCats()
{
IUserService userService = new UserServiceImpl();
return userService.getAllCats();
}
}
3 javabean:
package com.webservice.cxf.domain;
public class Cat
{
//一定要是包装类型,而不能是基本类型
private Integer id;
private String cat;
private String color;
public Cat(Integer id, String cat, String color)
{
super();
this.id = id;
this.cat = cat;
this.color = color;
}
//一定要加上空构造
public Cat()
{
}
//set/get方法
}
package com.webservice.cxf.domain;
public class User
{
//一定要是包装类型,而不能是基本类型
private Integer id;
private String name;
private String pass;
private String address;
//一定要加上空构造
public User()
{
}
public User(Integer id, String name, String pass, String address)
{
super();
this.id = id;
this.name = name;
this.pass = pass;
this.address = address;
}
//set/get方法....
//重写hashCode和equals,只要用户名和密码一致就认为是一个对象
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((pass == null) ? 0 : pass.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (name == null)
{
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (pass == null)
{
if (other.pass != null)
return false;
} else if (!pass.equals(other.pass))
return false;
return true;
}
}
4 service接口:
package com.webservice.cxf.service;
import java.util.List;
import java.util.Map;
import com.webservice.cxf.domain.Cat;
import com.webservice.cxf.domain.User;
public interface IUserService
{
List<Cat> getCatsByUser(User user);
Map<String, Cat> getAllCats();
}
5、service接口的实现方法,具体的业务逻辑:
package com.webservice.cxf.service.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.webservice.cxf.domain.Cat;
import com.webservice.cxf.domain.User;
import com.webservice.cxf.service.IUserService;
public class UserServiceImpl implements IUserService
{
//通过map来模拟数据库里的信息
static Map<User, List<Cat>> userDb = new HashMap<User, List<Cat>>();
static
{
List<Cat> catList = new ArrayList<Cat>();
catList.add(new Cat(1, "ketty", "白色"));
catList.add(new Cat(2, "加菲", "黄色"));
userDb.put(new User(1, "Tom", "1234", "tianjin"), catList);
List<Cat> catList2 = new ArrayList<Cat>();
catList2.add(new Cat(3, "机器", "蓝色"));
catList2.add(new Cat(4, "哈哈", "红色"));
userDb.put(new User(2, "Richard", "1234", "beijing"), catList2);
}
@Override
public List<Cat> getCatsByUser(User user)
{
return userDb.get(user);
}
@Override
public Map<String, Cat> getAllCats()
{
Map<String, Cat> catMap = new HashMap<String, Cat>();
int i = 1;
Collection<List<Cat>> catList = userDb.values();
for (List<Cat> list : catList)
{
for (Cat cat : list)
{
catMap.put("第" + i++ + "个", cat);
}
}
return catMap;
}
}
6、由于webservice不能够处理返回map集合的类型,自定义适配器将webservice 不能处理的类型转化为它能够处理的类型:
package com.webservice.cxf.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 com.webservice.cxf.domain.Cat;
import com.webservice.cxf.util.StringCat.Entity;
/**
* 由于webservice不能够处理返回map集合的类型,自定义适配器将webservice
* 不能处理的类型转化为它能够处理的类型
* XmlAdapter<StringCat, Map<String, Cat>> 后一个是不能处理的类型,前一个是可以处理的类型。
*/
public class XMLAdapter extends XmlAdapter<StringCat, Map<String, Cat>>
{
//将map类型转化为一个对象类型,对象类型为webservice可以处理的类型
@Override
public StringCat marshal(Map<String, Cat> v) throws Exception
{
StringCat stringCat = new StringCat();
for (String key : v.keySet())
{
stringCat.getEntities().add(new Entity(key, v.get(key)));
}
return stringCat;
}
//将一个对象类型转化为map类型
@Override
public Map<String, Cat> unmarshal(StringCat v) throws Exception
{
Map<String, Cat> result = new HashMap<String, Cat>();
for (Entity entity : v.getEntities())
{
result.put(entity.getKey(), entity.getValue());
}
return result;
}
}
class StringCat
{
static class Entity
{
private String key;
private Cat value;
public Entity()
{
}
public Entity(String key, Cat value)
{
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<Entity> entities = new ArrayList<StringCat.Entity>();
public List<Entity> getEntities()
{
return entities;
}
public void setEntities(List<Entity> entities)
{
this.entities = entities;
}
@Override
public String toString()
{
return "StringCat [entities=" + entities + ", getEntities()="
+ getEntities() + "]";
}
}
7、服务器启动并暴露接口:
import com.webservice.cxf.impl.HelloWorldImpl;
public class TestMain
{
public static void main(String[] args)
{
HelloWorldImpl helloWorldImpl = new HelloWorldImpl();
Endpoint.publish("http://127.0.0.1/richard", helloWorldImpl);
helloWorldImpl.sayHi("Richard");
System.out.println("暴漏成功");
}
}
客户端:在客户端只需要安装完毕CXF,并使用wsdl2java命令将服务器端的java或者class文件导入到客户端,客户端就可以像服务器端一样调用方法。客户端的方法调用:
package com.webservice.cxf.test; import javax.xml.ws.Endpoint;
package com.webservice.cxf.test; import java.util.List; import com.webservice.cxf.Cat; import com.webservice.cxf.Entity; import com.webservice.cxf.IHelloWorld; import com.webservice.cxf.StringCat; import com.webservice.cxf.User; import com.webservice.cxf.impl.HelloWorldImpl; public class ClientTestmain { public static void main(String[] args) { HelloWorldImpl factory = new HelloWorldImpl(); IHelloWorld hw = factory.getHelloWorldImplPort(); System.out.println(hw.sayHi("xushuai")); User user = new User(); user.setId(1); user.setName("Richard"); user.setPass("1234"); List<Cat> catList = hw.getCatsByUser(user); for (Cat cat : catList) { System.out.println(cat.getCat()); } StringCat stringCat = hw.getAllCats(); List<Entity> ens = stringCat.getEntities(); for (int i = 0; i < ens.size(); i++) { System.out.println(ens.get(i).getKey() + " | " + ens.get(i).getValue().getCat()); } } }
}