我正在使用 spring-mvc 和 Jaxb2Marshaller 开发 Web 服务。
我有两个类,都用相同的@XmlRootElement
名称注释
@XmlRootElement(name="request")
class Foo extends AstractRequest {
}
@XmlRootElement(name="request")
class Bar extends AbstractRequest {
}
所有三个类(AbstractRequest、Foo、Bar)都以相同的顺序包含在 classesToBeBound 列表中
现在使用 Bar 的请求工作正常。但是使用 Foo 的那个在使用消息解组期间抛出 ClassCastException 异常Bar cannot be cast to Foo
控制器代码是这样的,
Source source = new StreamSource(new StringReader(body));
Foo request = (Foo) this.jaxb2Marshaller.unmarshal(source);
我猜这是因为 Bar 是一种覆盖 Foo 的原因,因为它是在要绑定到 spring-servlet.xml 文件中的类列表中的 Foo 之后编写的
但是,我也有多个带有注释的类,@XmlRootElement(name="response")
并且编组响应没有任何问题。
有没有办法指定 jaxb2Marshaller 用于解组的类?