我希望使用外部绑定,这样就可以对同一个java类进行多个xml配置,而无需在代码中使用注释。问题是,我的程序似乎没有使用我使用的外部绑定文件,而只是进行默认的解组/编组。
我有以下课程:
Customer.java
@XmlRootElement(name="customer")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
private String firstName;
private String lastName;
private Address address;
private List<PhoneNumber> phoneNumbers;
//getters and settersAddress.java
public class Address {
private String street;
//getters and setters
}PhoneNumber.java
public class PhoneNumber {
private String type;
private String number;
//getters and setters
}Demo.java
public class Demo {
public static void main(String[] args) throws Exception {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream iStream = classLoader.getResourceAsStream("src/blog/bindingfile/binding.xml");
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
JAXBContext jc = JAXBContext.newInstance(new Class[] {Customer.class}, properties);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource xml = new StreamSource("src/blog/bindingfile/input.xml");
Customer customer = unmarshaller.unmarshal(xml, Customer.class).getValue();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(customer, System.out);
} 我还尝试了另一个版本的Demo.java
public class Demo {
public static void main(String[] args) throws Exception {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream iStream = classLoader.getResourceAsStream("src/blog/bindingfile/binding.xml");
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
JAXBContext jc = JAXBContext.newInstance(new Class[] {Customer.class} , properties);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Customer customer = (Customer) unmarshaller.unmarshal(new File("src/blog/bindingfile/input.xml"));
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(customer, System.out);
}我的Binding.xml如下:
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="blog.bindingfile">
<xml-schema
namespace="http://www.example.com/customer"
element-form-default="QUALIFIED"/>
<java-types>
<java-type name="Customer">
<xml-root-element/>
<xml-type prop-order="firstName lastName address phoneNumbers"/>
<java-attributes>
<xml-element java-attribute="firstName" name="first-name"/>
<xml-element java-attribute="lastName" name="last-name"/>
<xml-element java-attribute="phoneNumbers" name="phone-number"/>
</java-attributes>
</java-type>
<java-type name="PhoneNumber">
<java-attributes>
<xml-attribute java-attribute="type"/>
<xml-value java-attribute="number"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>input.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<customer>
<first-name>Jane</first-name>
<last-name>Doe</last-name>
<address>
<street>123 A Street</street>
</address>
<phone-number type="work">555-1111</phone-number>
<phone-number type="cell">555-2222</phone-number>
</customer>无论我使用什么Demo.java输出,都是:
<?xml version="1.0" encoding="UTF-8"?>
<customer>
<address>
<street>123 A Street</street>
</address>
</customer>有人能告诉我我做错了什么吗?我注意到,无论我作为binding.xml传入什么(即使文件不存在),它也不会抱怨。
发布于 2015-10-15 18:19:43
问题就在这条线上:
InputStream iStream = classLoader.getResourceAsStream("src/blog/bindingfile/binding.xml");iStream为空。因为程序找不到文件。但是,它不会抛出任何异常。
然后将这个空InputStream传递给
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
JAXBContext jc = JAXBContext.newInstance(new Class[] {Customer.class} , properties)同样,不会引发异常,因此解组/编组程序使用默认规则进行解组/编组,因此我们在问题中显示了xml输出。
通过在src文件夹级别添加一个新文件夹(资源),然后将binding.xml文件移到那里,修复了文件错误。然后路径是resources/binding.xml。即使使用完整的路径对我也不起作用
https://stackoverflow.com/questions/33112876
复制相似问题