class XXX nor any of its super class is known to this context.

 用CXF传递复杂的数据类型,比如dto

@XmlType(name = "Customer")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
	private long id;
	private String name;
	private Date birthday;
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
}

 interface

 

@WebService
public interface TestService {
	String testString(String name);
	int testInt(int a, int b);
	List<String> testList(List<String> list);
	Customer testObject(Customer c);
}

 实现类

public Customer testObject(Customer c) {
		Customer ct = new Customer();
		ct.setId(2L);
		ct.setName("11");
		ct.setBirthday(new Date());
		return ct;
	}

 测试类

public void testObject() {
		try {
			Customer ct = new Customer();
			ct.setId(1L);
			ct.setName("33");
			ct.setBirthday(new Date());
			Object obj[] = client.invoke("testObject", ct);
			Customer cc = (Customer) obj[0]; 
			System.out.println(ct.getBirthday());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

 

当调用的时候,报错

Caused by: javax.xml.bind.JAXBException: class com.infodms.ws.dto.Customer nor any of its super class is known to this context.
	at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:594)
	at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:648)
	... 41 more

 上网搜了一下,找不到合理的解释,把testObject的参数去掉,只留下返回值,报错

java.lang.ClassCastException: com.infodms.ws.test.Customer cannot be cast to com.infodms.ws.dto.Customer

 奇怪了Customer类明明是dto包中的类怎么cxf认为是test包中的类,原来接口和实现类都是test包中的类,cxf在object和xml转换的时候会默认在同一个包下面找dto,如果找不到则报错,把这个dto跟接口和实现类放到同一个包下面,问题解决。

 

 

我如果不想把dto和接口放到同一个包下面又该怎么办呢?我们需要在dto的上面定义它的namespace,注意这个字符串跟包名的顺序是相反的

@XmlType(name = "Customer", namespace = "dto.ws.infodms.com")

 问题解决。

 

 

 

你可能感兴趣的:(C++,c,xml,webservice,sun)