JAXB MOXy外部绑定文件未被读取 [英] JAXB MOXy external bindings file not being read

查看:73
本文介绍了JAXB MOXy外部绑定文件未被读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用外部绑定,这样我就可以为相同的java类提供多个xml配置,而无需在代码中使用注释。问题是我的程序似乎没有使用我使用的外部绑定文件,而只是默认解组/编组。

I want to use external bindings so that I can have multiple xml configurations for the same java classes without using annotations in the code. The problem is my program does not seem to use the external bindings file that I use and instead just does default unmarshalling/marshalling.

我有以下类:

Customer.java

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 setters

Address.java

Address.java

public class Address {

    private String street;
    //getters and setters
}

PhoneNumber.java

PhoneNumber.java

public class PhoneNumber {

    private String type;
    private String number;
    //getters and setters
}

Demo.java

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

I also tried an alternative version of 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如下:

My Binding.xml is as follows:

<?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>

输入文件是:

<?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我使用的输出是:

Whatever Demo.java I use the output I get is:

<?xml version="1.0" encoding="UTF-8"?>
<customer>
   <address>
      <street>123 A Street</street>
   </address>
</customer>

有人可以告诉我我做错了什么。我注意到无论我传递什么作为binding.xml(即使该文件不存在),它也不会抱怨。

Can someone please tell me what I am doing wrong. I noticed that no matter what I pass in as the binding.xml (even if the file doesn't exist) it doesn't complain.

推荐答案

问题在于这一行:

InputStream iStream = classLoader.getResourceAsStream("src/blog/bindingfile/binding.xml");

iStream为空。因为程序找不到该文件。但是它不会抛出任何异常。

iStream was null. Because the program couldn't find the file. However it will not throw any exceptions.

然后这个空的InputStream被传递到

Then this null InputStream was passed into

    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
    JAXBContext jc = JAXBContext.newInstance(new Class[] {Customer.class} , properties)

同样没有抛出异常,因此unmarshaller / marshaller使用默认规则进行解组/编组,因此我们得到问题中显示的xml输出。

Again no exceptions are thrown, therefore the unmarshaller/marshaller does the unmarshalling/marshalling using the default rules and so we get the xml output shown in the question.

通过在src文件夹级别添加新文件夹(资源)然后在那里移动binding.xml文件来修复文件错误。然后路径是resources / binding.xml。即使使用完整路径也不适合我

The file error was fixed by adding a new folder (resources) at the src folder level and then moving the binding.xml file there. Then the path was resources/binding.xml. Even using the full path did not work for me

这篇关于JAXB MOXy外部绑定文件未被读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆