nai*_*vin 10 spring-mvc jaxb jaxb2
我正在使用spring-mvc和Jaxb2Marshaller开发Web服务.
我有两个类,都注释了相同的@XmlRootElement名称
@XmlRootElement(name="request")
class Foo extends AstractRequest {
}
@XmlRootElement(name="request")
class Bar extends AbstractRequest {
}
Run Code Online (Sandbox Code Playgroud)
所有三个类(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);
Run Code Online (Sandbox Code Playgroud)
我想这种情况正在发生,因为Bar有点覆盖Foo,因为它是在Spring的servlet.xml文件中绑定的类列表中的Foo之后编写的
但是我也有多个带有注释的类,@XmlRootElement(name="response")并且编组响应不会产生任何问题.
有没有办法指定jaxb2Marshaller用于解组的类?
您可以从 Jaxb2Marshaller 创建 Unmarshaller,然后可以将要解组的类作为参数传递给采用 Source 的解组方法:
Source source = new StreamSource(new StringReader(body));
Unmarshaller unmarshaller = jaxb2Marshaller.createUnmarshaller();
Foo request = (Foo) unmarshaller.unmarshal(source, Foo.class).getValue();
Run Code Online (Sandbox Code Playgroud)
欲了解更多信息,请参阅:
| 归档时间: |
|
| 查看次数: |
8720 次 |
| 最近记录: |