package cxf.ws;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import cxf.ws.domain.Cat;
import cxf.ws.domain.User;
import cxf.ws.utils.FKXmlAdapter;
@WebService
public interface HelloWorld {
String sayHi(String name);
List<Cat> getCatByUser(User user);
@XmlJavaTypeAdapter((FKXmlAdapter.class)) Map<String,Cat> getAllCats();
}
package cxf.ws.impl;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import cxf.ws.HelloWorld;
import cxf.ws.domain.Cat;
import cxf.ws.domain.User;
import cxf.ws.service.UserService;
import cxf.ws.service.impl.UserServiceImpl;
@WebService(endpointInterface="cxf.ws.HelloWorld",serviceName="HelloWorldWS")
public class HelloWorldWS implements HelloWorld {
@Override
public String sayHi(String name) {
return name+",您好,"+"现在时间是:"+new Date();
}
/**
* 在实际的应用中,webService只会调用业务组件,暴露接口。
*/
@Override
public List<Cat> getCatByUser(User user) {
UserService us=new UserServiceImpl();
return us.getCatByUser(user);
}
@Override
public Map<String, Cat> getAllCats() {
UserService us=new UserServiceImpl();
return us.getAllCats();
}
}
package cxf.ws.service;
import java.util.List;
import java.util.Map;
import cxf.ws.domain.Cat;
import cxf.ws.domain.User;
public interface UserService {
List<Cat> getCatByUser(User user);
Map<String, Cat> getAllCats();
}
package cxf.ws.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cxf.ws.domain.Cat;
import cxf.ws.domain.User;
import cxf.ws.service.UserService;
public class UserServiceImpl implements UserService {
static HashMap<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,"feyman","黄色"));
catList1.add(new Cat(3,"贝儿","蓝色"));
catDb.put(new User(1,"dudu","duyx5218","link Street"), catList1);
List<Cat> catList2=new ArrayList<Cat>();
catList2.add(new Cat(4,"Joe","黄色"));
catList2.add(new Cat(5,"Grace","粉色"));
catDb.put(new User(2,"Janey","202053","link Street"), catList2);
}
@Override
public List<Cat> getCatByUser(User user) {
return catDb.get(user);
}
@Override
public Map<String, Cat> getAllCats() {
HashMap<String,Cat> map=new HashMap<String,Cat>();
int i=1;
for(List<Cat> catList:catDb.values()){
for(int index=0;index<catList.size();index++){
map.put("第"+(i++)+"只猫", catList.get(index));
}
}
return map;
}
public static void main(String[] args) {
HashMap<String,Cat> map=(HashMap<String, Cat>) new UserServiceImpl().getAllCats();
for(String key:map.keySet()){
System.out.println(key+":信息: 【"+map.get(key).getName()+":"+map.get(key).getColor()+"】");
}
}
}
package cxf.ws.utils;
import java.util.ArrayList;
import java.util.List;
import cxf.ws.domain.Cat;
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;
}
public Entry(String key, Cat value) {
super();
this.key = key;
this.value = value;
}
public Entry() {
super();
}
}
private List<Entry> entries=new ArrayList<StringCat.Entry>();
public List<Entry> getEntries() {
return entries;
}
public void setEntries(List<Entry> entries) {
this.entries = entries;
}
}
package cxf.ws.utils;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import cxf.ws.domain.Cat;
import cxf.ws.utils.StringCat.Entry;
public class FKXmlAdapter extends XmlAdapter<StringCat, Map<String, Cat>> {
@Override
public Map<String, Cat> unmarshal(StringCat v) throws Exception {
Map<String,Cat> map=new HashMap<String,Cat>();
for(Entry entry:v.getEntries()){
map.put(entry.getKey(), entry.getValue());
};
return map;
}
@Override
public StringCat marshal(Map<String, Cat> v) throws Exception {
StringCat sc=new StringCat();
for(String key:v.keySet()){
sc.getEntries().add(new Entry(key,v.get(key)));
}
return sc;
}
}
package cxf.ws.utils;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import cxf.ws.domain.Cat;
import cxf.ws.utils.StringCat.Entry;
public class FKXmlAdapter extends XmlAdapter<StringCat, Map<String, Cat>> {
@Override
public Map<String, Cat> unmarshal(StringCat v) throws Exception {
Map<String,Cat> map=new HashMap<String,Cat>();
for(Entry entry:v.getEntries()){
map.put(entry.getKey(), entry.getValue());
};
return map;
}
@Override
public StringCat marshal(Map<String, Cat> v) throws Exception {
StringCat sc=new StringCat();
for(String key:v.keySet()){
sc.getEntries().add(new Entry(key,v.get(key)));
}
return sc;
}
}
package cxf.ws.domain;
public class User {
Integer id;
String name;
String pass;
String address;
public User(Integer id, String name, String pass, String address) {
super();
this.id = id;
this.name = name;
this.pass = pass;
this.address = address;
}
public User() {
super();
}
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 getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@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;
}
}
package lee;
import javax.xml.ws.Endpoint;
import cxf.ws.HelloWorld;
import cxf.ws.impl.HelloWorldWS;
public class ServerMain {
public static void main(String[] args) {
HelloWorld hw=new HelloWorldWS();
Endpoint.publish("http://10.0.6.17/fightUp", hw);
System.out.println("web service 暴露成功");
}
}
执行serverMain的main方法时,报错:
错误是:
Exception in thread "main" javax.xml.ws.WebServiceException: java.lang.IllegalArgumentException: value class cxf.ws.utils.FKXmlAdapter
我按照网上的视频敲的代码,望高人指点,谢谢。
已得到解决:自己的jar文件版本太高,换成低版本就可以了,唉!