CXF学习-处理CXF不能处理的类型

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文件版本太高,换成低版本就可以了,唉!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值