简体   繁体   English

将Jaxb2Marshaller与具有相同@XmlRootElement名称的多个类一起使用

[英]Using Jaxb2Marshaller with multiple classes having same @XmlRootElement name

I am working on a web service using spring-mvc and Jaxb2Marshaller. 我正在使用spring-mvc和Jaxb2Marshaller开发Web服务。

I have two classes, both annotated with the same @XmlRootElement name 我有两个类,都用相同的@XmlRootElement名称注释

@XmlRootElement(name="request")
class Foo extends AstractRequest {

}

@XmlRootElement(name="request")
class Bar extends AbstractRequest {

}

All three classes (AbstractRequest, Foo, Bar) are included in the classesToBeBound list in the same order 所有三个类(AbstractRequest,Foo,Bar)以相同的顺序包含在classesToBeBound列表中

Now the request that uses Bar works fine. 现在,使用Bar的请求可以正常工作。 But the one that uses Foo throws a ClassCastException exception during unmarshalling with message Bar cannot be cast to Foo 但是,使用Foo的用户在将消息Bar cannot be cast to Foo解组时Bar cannot be cast to Foo抛出ClassCastException异常。

The controller code is this, 控制器代码是这个,

Source source = new StreamSource(new StringReader(body));
Foo request = (Foo) this.jaxb2Marshaller.unmarshal(source); 

I guess this is happening because Bar is kind of overriding Foo since it's written after Foo in the list of classes to be bound in the spring-servlet.xml file 我猜 发生了这种情况,因为Bar是覆盖Foo的,因为它是在Foo之后写在spring-servlet.xml文件中要绑定的类列表中的

However I am also having multiple classes annotated with @XmlRootElement(name="response") and marshalling the response doesn't give any problem. 但是我也有多个用@XmlRootElement(name="response")注释的类,并且整理响应不会带来任何问题。

Is there a way to specify the class to be used by the jaxb2Marshaller for unmarshalling ? 有没有办法指定jaxb2Marshaller用于解组的类?

You can pass the class to Jaxb2Marshaller before unmarshal: 您可以在解组之前将类传递给Jaxb2Marshaller:

Source source = new StreamSource(new StringReader(body));
jaxb2Marshaller.setMappedClass(Foo.class);

Foo request = (Foo) jaxb2Marshaller.unmarshal(source);

You can create an Unmarshaller from the Jaxb2Marshaller, then you can pass the class you want to unmarshal as a parameter to the unmarshal method that takes a Source: 您可以从Jaxb2Marshaller创建一个Unmarshaller,然后可以将要解组的类作为参数传递给采用Source的unmarshal方法:

Source source = new StreamSource(new StringReader(body));
Unmarshaller unmarshaller = jaxb2Marshaller.createUnmarshaller();
Foo request = (Foo) unmarshaller.unmarshal(source, Foo.class).getValue(); 

For more information see: 有关更多信息,请参见:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Spring MVC控制器中注入Jaxb2Marshaller - Injecting Jaxb2Marshaller in Spring MVC controller Spring Jaxb2Marshaller外部绑定文件 - Spring Jaxb2Marshaller External Binding File 无法提取响应:jaxb2marshaller没有合适的HttpMessageConverter - Could not extract response: no suitable HttpMessageConverter with jaxb2marshaller 带有Jaxb2Marshaller的Spring MVC在2013年日期失败 - Spring MVC with Jaxb2Marshaller failing on 2013 dates 如何配置mvc:annotation-driven使用的Jaxb2Marshaller - How can I configure the Jaxb2Marshaller used by mvc:annotation-driven Spring MVC-使用@ResponseBody时设置JAXB marshaller属性 - Spring MVC - set JAXB marshaller property when using @ResponseBody JAXB - Marshaller 写入系统输出但不写入 XML 文件 - JAXB - Marshaller Writing to System Output But Not to XML File 在端点处理程序中访问jaxb2 marshaller(spring ws) - access the jaxb2 marshaller in endpoint handler (spring ws) 拥有多个异常类不像冗余,尤其是当我们使用错误代码时? - Having Multiple Exception classes aren't like Redundant, especially when we are using Error Codes? 如何使用单个查询从具有不同元素的JSP向表中的相同ID插入多个记录 - how to insert multiple records against same ID in a table from JSP having different elements using single query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM