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.