阅读背景:

将Jaxb pojo序列化为多个或不同的名称空间

来源:互联网 

Consider the following code:

考虑下面的代码:

Main.java
====
package com.sample;

import com.sample.entity.Customer;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Main {    

    public static void main(String[] args) throws JAXBException {
       JAXBContext jc = JAXBContext.newInstance(Customer.class);

       Customer customer = new Customer();
       customer.setId(123);

       Marshaller m = jc.createMarshaller();
       m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
       m.marshal(customer, System.out);    
    }
}


Customer.java
====
package com.sample.entity;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Customer {
    private long id;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
}

package-info.java
====
@XmlSchema(namespace = "https://www.example.org/package", elementFormDefault = XmlNsForm.QUALIFIED)
package com.sample.entity;

import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.XmlNsForm;
Main.



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: