[英]Supporting multiple xsd versions
Let me explain my scenario I have a web application that takes xml requests and responds. 让我解释一下我的情况,我有一个Web应用程序,它接受xml请求并做出响应。 I have used jaxb to parse the request, convert it to my applications dao object, process the request and then reconvert the dao result to jaxb object and return that.
我已经使用jaxb解析请求,将其转换为我的应用程序dao对象,处理请求,然后将dao结果转换为jaxb对象并返回。 Now i have to support multiple versions (different xml versions can come as request and i have to respond with suitable xml response).
现在,我必须支持多个版本(可以根据请求提供不同的xml版本,并且我必须以适当的xml响应进行响应)。 I generate java files for jaxb object -> dao object convertor and reverse.
我为jaxb对象-> dao对象转换器生成Java文件并反转。 Problem is that before conversion to my dao object the jaxb object is created and then validated before passing it to my convertor.
问题在于,在转换为dao对象之前,先创建jaxb对象,然后对其进行验证,然后再将其传递给我的转换器。 I am not sure what design pattern to apply here.
我不确定要在此处应用哪种设计模式。 Might be adaptor pattern.
可能是适配器模式。 Please suggest?
请提出建议? Also throw light if i may have to generate the jaxb marshalling and un-marshalling code too?
如果我可能也必须生成jaxb编组和解组代码,也请注意一下?
Hopefully I understood well your problem. 希望我能很好地理解您的问题。 So you already have:
因此,您已经拥有:
xml 1 ---> jaxb objects 1 ---> converter 1 ---> entity objects
And now you need to acomodate a new xml format. 现在,您需要调整新的xml格式。
For this you can either write a new converter to support the new format: 为此,您可以编写一个新转换器以支持新格式:
xml 2 ---> jaxb objects 2 ---> converter 2 ---> entity objects
or write some xml adapters to adapt the new jaxb objects to the first format: 或编写一些xml适配器以使新的jaxb对象适应第一种格式:
xml 2 ---> jaxb objects 2 ---> adapter ---> jaxb objects 1 ---> converter 1 ---> entity objects
I would suggest the first approach: to write a new converter so the new format is not dependent of the first one (in case might need to retire first format). 我建议第一种方法:编写一个新的转换器,以便新格式不依赖于第一种格式(以防万一可能需要停用第一种格式)。
The converter mixes itself with the jaxb objects as a wrapper around the entity objects together with the @XmlJavaTypeAdapter
for complex types. 转换器将自己与jaxb对象混合在一起,作为实体对象的包装,
@XmlJavaTypeAdapter
为复杂类型提供@XmlJavaTypeAdapter
。 A very simple example: 一个非常简单的例子:
public class XmlPerson {
private Person person;
public XmlPerson(Person person) {
this.person = person;
}
public XmlPerson() {
this.person = new Person();
}
public Person getPerson() {
}
public String getFirstName() {
return person.getFristname();
}
public void setFirstname(String firstName) {
person.setFirstName(firstName);
}
....
@XmlJavaTypeAdapter(XmlAddressAdapter.class)
public Address getAddress() {
return person.getAddress();
}
public void setAddress(Address address) {
person.setAddress(address);
}
}
The @XmlJavaTypeAdapter
should itself adapt(convert) from an Address
to a XmlAddress
. @XmlJavaTypeAdapter
本身应该从Address
适应(转换)为XmlAddress
。
public class XmlAddressAdapter extends XmlAdapter<XmlAddress, Address> {
@Override
public Address unmarshal(XmlAddress xmlAddress) throws Exception {
return xmlAddress.getAddress();
}
@Override
public XmlAddress marshal(Address address) throws Exception {
return address != null ? new XmlAddress(address) : null;
}
}
Also throw light if i may have to generate the jaxb marshalling and un-marshalling code too?
如果我可能也必须生成jaxb编组和解组代码,也请注意一下?
You just need to pass the xml objects to the marshaller and validate the xsd
with the unmarshaller as yoou might already be doing: 您只需要将xml对象传递给编组器,并使用解组器验证
xsd
,因为您可能已经在这样做了:
Marshal: 元帅:
JAXBContext jaxbContext = JAXBContext.newInstance(XmlPerson.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.marshal(xmlPerson, output);
Unmarshal: 解组:
JAXBContext jaxbContext = JAXBContext.newInstance(targetClass);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
SchemaFactory sf = SchemaFactory.newInstance(
javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(XmlInvoice.class.getResource("/yourXsd.xsd"));
jaxbUnmarshaller.setSchema(schema);
Object unmarshalResult = jaxbUnmarshaller.unmarshal(inputStream);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.