目录
JAXB Annotation
maven依赖
举例
工具类
参考
JAXB Annotation
用来根据java类生成xml内容
1、@XmlRootElement // xml 文件的根元素(顶层类),将类或枚举类型映射到 XML 元素
属性:
该注解含有name和namespace两个属性。namespace属性用于指定生成的元素所属的命名空间。
2、@XmlElement
作用和用法:字段,方法,参数级别的注解。该注解可以将被注解的字段(非静态),或者被注解的get/set方法对应的字段映射为本地元素,也就是子元素。默认使用字段名或get/set方法去掉前缀剩下部分小写作为元素名(在字段名和get/set方法符合命名规范的情况下)。
3、@XmlAccessorType // 控制默认情况下是否对字段或 Javabean 属性进行系列化
默认规则:
默认情况下,如果包中不存在 @XmlAccessorType,那么假定使用以下包级别注释。
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
默认情况下,如果类中不存在 @XmlAccessorType,并且没有任何超类是使用 @XmlAccessorType 注释的,则假定在类中使用默认注释
可能值:
FIELD: JAXB 绑定类中的每个非静态、非瞬态字段将会自动绑定到 XML,除非由 XmlTransient 注释。
NONE: 所有字段或属性都不能绑定到 XML,除非使用一些 JAXB 注释专门对它们进行注释。
PROPERTY: JAXB 绑定类中的每个get()、set()方法对将会自动绑定到 XML,除非由 XmlTransient 注释。还有, 如果类变量没有 get、set方法),那么不能转换成标签。
PUBLIC_MEMBER:每个公共get()、set()方法对和每个公共字段将会自动绑定到 XML,除非由 XmlTransient 注释
4、@XmlTransient
作用和用法:
类,字段,方法级别的注解。可使JAXB在映射xml元素时忽略被注解的类,字段,get/set对应字段。需要注意的是该注解与所有其他JAXB注释相互排斥,也就是说与其他注释连用就会报错。
5、@XmlJavaTypeAdaptor
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Boy {
private String name = "CY";
private Address address; // 是一个接口
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在 java object 转换成 xml 的时候,接口Address 无法被转换。
所以 这里要加上 @XmlJavaTypeAdapter(AddressAdapter.class)
所以 要多写一个AddressAdaptor 类。
这个类会返回Address接口的一个具体实现类的对象。
public class AddressAdapter extends XmlAdapter{ @Override public Address unmarshal(String date) throws Exception { return new Address; } @Override public String marshal(Address adress) throws Exception { return adress.toString; } }
maven依赖
javax.xml.bind
jaxb-api
2.3.1
举例
import lombok.Data;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "TX")
public class Request {
@XmlElement(name = "REQUEST_SN")
private String requestSn; // 请求序列号
@XmlElement(name = "TX_INFO")
private TxInfo txInfo;
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public static class TxInfo {
@XmlElement(name = "DATE")
private String date;
}
}
工具类
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
@SuppressWarnings({"rawtypes", "unchecked"})
public static T xml2Object(Class T, String xmlString) {
try {
if (Strings.isNullOrEmpty(xmlString)) {
return null;
}
JAXBContext jaxbContext = JAXBContext.newInstance(T);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
ByteArrayInputStream is = new ByteArrayInputStream(xmlString.getBytes(StandardCharsets.UTF_8));
return (T) unmarshaller.unmarshal(is);
} catch (Exception e) {
log.error("Jaxb unmarshal exception. " + e.getMessage(), e);
return null;
}
}
public static String object2Xml(Object object) {
Preconditions.checkNotNull(object);
return object2Xml(object.getClass(), object);
}
@SuppressWarnings("rawtypes")
public static String object2Xml(Class targetClass, Object object) {
return object2Xml(targetClass, object, true, false, false);
}
@SuppressWarnings("rawtypes")
public static String object2Xml(Class targetClass, Object object, boolean formatted, boolean noXmlHeader, boolean noEscape) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
JAXBContext jaxbContext = JAXBContext.newInstance(targetClass);
Marshaller marshaller = jaxbContext.createMarshaller();
if (formatted) {
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
}
if (noXmlHeader) {
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
}
if (noEscape) {
marshaller.setProperty(CharacterEscapeHandler.class.getName(), new CharacterEscapeHandler() {
public void escape(char[] ch, int start, int length, boolean isAttVal, Writer writer) throws IOException {
writer.write(ch, start, length);
}
});
}
marshaller.marshal(object, baos);
} catch (JAXBException e) {
log.error("Jaxb marshal exception. " + e.getMessage(), e);
return null;
}
return baos.toString();
}
参考
import lombok.Data;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "TX")
public class Request {
@XmlElement(name = "REQUEST_SN")
private String requestSn; // 请求序列号
@XmlElement(name = "TX_INFO")
private TxInfo txInfo;
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public static class TxInfo {
@XmlElement(name = "DATE")
private String date;
}
}
工具类
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
@SuppressWarnings({"rawtypes", "unchecked"})
public static T xml2Object(Class T, String xmlString) {
try {
if (Strings.isNullOrEmpty(xmlString)) {
return null;
}
JAXBContext jaxbContext = JAXBContext.newInstance(T);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
ByteArrayInputStream is = new ByteArrayInputStream(xmlString.getBytes(StandardCharsets.UTF_8));
return (T) unmarshaller.unmarshal(is);
} catch (Exception e) {
log.error("Jaxb unmarshal exception. " + e.getMessage(), e);
return null;
}
}
public static String object2Xml(Object object) {
Preconditions.checkNotNull(object);
return object2Xml(object.getClass(), object);
}
@SuppressWarnings("rawtypes")
public static String object2Xml(Class targetClass, Object object) {
return object2Xml(targetClass, object, true, false, false);
}
@SuppressWarnings("rawtypes")
public static String object2Xml(Class targetClass, Object object, boolean formatted, boolean noXmlHeader, boolean noEscape) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
JAXBContext jaxbContext = JAXBContext.newInstance(targetClass);
Marshaller marshaller = jaxbContext.createMarshaller();
if (formatted) {
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
}
if (noXmlHeader) {
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
}
if (noEscape) {
marshaller.setProperty(CharacterEscapeHandler.class.getName(), new CharacterEscapeHandler() {
public void escape(char[] ch, int start, int length, boolean isAttVal, Writer writer) throws IOException {
writer.write(ch, start, length);
}
});
}
marshaller.marshal(object, baos);
} catch (JAXBException e) {
log.error("Jaxb marshal exception. " + e.getMessage(), e);
return null;
}
return baos.toString();
}
参考
JAXB常用注解讲解(超详细)_@不会Ayy的拉马尔的博客-CSDN博客_jaxb
JAXB Marshal without @XmlRootElement - Missing @XmlRootElement



