JAXB--obj2xml&xml2obj
参考:
https://www.cnblogs.com/mumuxinfei/p/8948299.html 去掉 xsi:type="echoBody" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
测试
public void test6() throws Exception {
LogisticsOrderListModel model =new LogisticsOrderListModel();
model.setAccessCode("111111111");
JAXBContext jaxbContext =JAXBContext.newInstance(model.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();
//marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
StringWriter stringWriter=new StringWriter();
marshaller.marshal(model, stringWriter);
String result=stringWriter.toString();
System.out.println(result);
com.sf.wms.sao.dto.Request request=new com.sf.wms.sao.dto.Request();
request.setBody(new com.sf.wms.sao.dto.Request.Body(model));
System.out.println(JAXBUtil.writeToString(request,request.getClass(),model.getClass()));
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Request")
@Data
public class Request { private String service; private String lang; private String head; @XmlElement(name="body")
private Body body; @XmlAccessorType(XmlAccessType.FIELD)
@Data
@AllArgsConstructor
public static class Body {
@XmlAnyElement(lax = true)
private Object order;
}
}
@Request("OrderService")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "order")
@Data
public class LogisticsOrderListModel {
/**
* 订单个数
*/
private Integer orderCount;
/**
* 支持一个或多个订单
*/
private List<LogisticsOrderModel> orderList;
/**
* 顾客编码
*/
@XmlElement
private String accessCode;
注意
1:<?xml version="1.0" encoding="UTF-8"?> 的生成
2.去掉 xsi:type="echoBody" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
工具类
1. spring中提供的convert Jaxb2RootElementHttpMessageConverter
2.自定义的工具类
注意:由于使用了缓存,缓存的key为第一个class,一般应该把泛型的类或Object类放在第一位
JAXBUtil.writeToString(model.getClass(),request,request.getClass())
public class JAXBUtil {
private static XMLOutputFactory xmlOutputFactory=XMLOutputFactory.newFactory();;
public static String writeToString(Object o,Class...clazz) throws Exception {
try {
Marshaller marshaller = createMarshaller(clazz);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(baos, (String) marshaller.getProperty(Marshaller.JAXB_ENCODING));
xmlStreamWriter.writeStartDocument((String) marshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0");
marshaller.marshal(o, xmlStreamWriter);
xmlStreamWriter.writeEndDocument();
xmlStreamWriter.close();
return new String(baos.toByteArray());
}
catch (MarshalException ex) {
throw ex;
}
catch (JAXBException ex) {
throw new HttpMessageConversionException("Invalid JAXB setup: " + ex.getMessage(), ex);
}
}
public static String writeToString(Object o) throws Exception {
try {
Class<?> clazz = ClassUtils.getUserClass(o);
Marshaller marshaller = createMarshaller(clazz);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
//设置输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(baos);
xmlStreamWriter.writeStartDocument((String) marshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0");
marshaller.marshal(o, xmlStreamWriter);
xmlStreamWriter.writeEndDocument();
xmlStreamWriter.close();
return new String(baos.toByteArray());
}
catch (MarshalException ex) {
throw ex;
}
catch (JAXBException ex) {
throw new HttpMessageConversionException("Invalid JAXB setup: " + ex.getMessage(), ex);
}
}
private static final ConcurrentMap<Class<?>, JAXBContext> jaxbContexts = new ConcurrentHashMap<>(64);
/**
* Create a new {@link Marshaller} for the given class.
* @param clazz the class to create the marshaller for
* @return the {@code Marshaller}
* @throws HttpMessageConversionException in case of JAXB errors
*/
protected static Marshaller createMarshaller(Class<?>... clazz) {
try {
JAXBContext jaxbContext = getJaxbContext(clazz);
Marshaller marshaller = jaxbContext.createMarshaller();
customizeMarshaller(marshaller);
return marshaller;
}
catch (JAXBException ex) {
throw new HttpMessageConversionException(
"Could not create Marshaller for class [" + clazz + "]: " + ex.getMessage(), ex);
}
}
protected static void customizeMarshaller(Marshaller marshaller) {
}
protected static Unmarshaller createUnmarshaller(Class<?> clazz) {
try {
JAXBContext jaxbContext = getJaxbContext(clazz);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
customizeUnmarshaller(unmarshaller);
return unmarshaller;
}
catch (JAXBException ex) {
throw new HttpMessageConversionException(
"Could not create Unmarshaller for class [" + clazz + "]: " + ex.getMessage(), ex);
}
}
protected static void customizeUnmarshaller(Unmarshaller unmarshaller) {
}
protected static JAXBContext getJaxbContext(Class<?>... clazz) {
Assert.notNull(clazz, "Class must not be null");
JAXBContext jaxbContext = jaxbContexts.get(clazz);
if (jaxbContext == null) {
try {
jaxbContext = JAXBContext.newInstance(clazz);
jaxbContexts.putIfAbsent(clazz[0], jaxbContext);
}
catch (JAXBException ex) {
throw new HttpMessageConversionException(
"Could not instantiate JAXBContext for class [" + clazz + "]: " + ex.getMessage(), ex);
}
}
return jaxbContext;
}
}
JAXB--obj2xml&xml2obj的更多相关文章
- jaxb
一.简介 JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实 ...
- JavaEE学习之JAXB
一.前言 JAXB——Java Architecture for XML Binding,是一项可以根据XML Schema产生Java类的技术.JAXB提供将XML实例文档反向生成Java对象树的方 ...
- XmlRootElement JAXB
http://desert3.iteye.com/blog/1570092(文章已经很好) 看了那边文章以后尝试后写点直白的 PROPERTY: JAXB 绑定类中的每个获取方法/设置方法对将会自动绑 ...
- 错误:java.util.Map is an interface, and JAXB can't handle interfaces.
问题: 在整合spring+cxf时报错java.util.Map is an interface, and JAXB can't handle interfaces. 解决方法: 将服务端的serv ...
- Jaxb annotation使用
JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例文档反向 ...
- JAXB最佳实践
JAXB主要用来实现对象和XML之间的序列化和反序列化. 本文主要总结JAXB基本使用方法和注意事项! 通过下文的XML示例内容进行JAXB的简单实践 <?xml version="1 ...
- 在Gradle中使用jaxb的xjc插件
jaxb,全称为Java Architecture for Xml Binding,是一种将java对象与xml建立起映射的技术.其主要提供两个功能,一是将java对象映射为xml,二是将xml映射为 ...
- Java for XML: JAXP、JAXB、JAXM、JAX-RPC、JAX-WS
在XML领域里,对XML文件的校验有两种方式:DTD校验.Schema校验.在Java中,对于XML的解析,有多种方式:DOM解析.SAX解析.StAX解析.结合XML和Java后,就产生了Bind技 ...
- XStream、JAXB 日期(Date)、数字(Number)格式化输出xml
XStream.Jaxb是java中用于对象xml序列化/反序列化 的经典开源项目,利用它们将对象转换成xml时,经常会遇到日期(Date).数字按指定格式输出的需求,下面是使用示例: 一.日期字段格 ...
- java JAXB 学习
JAXB(Java Architecture for XML Binding)是JDK的一部分,用于Object <-> XML的转换(有点类似于.NET中的XML序列化). 1.创建XS ...
随机推荐
- java - 锁的种类及详解
锁类型 锁根据其特性能够划分出各种各样的锁类型,该文主要介绍以下锁的作用及特性 乐观锁/悲观锁 独享锁/共享锁 互斥锁/读写锁 可重入锁 公平锁/非公平锁 分段锁 偏向锁/轻量级锁/重量级锁 自旋锁 ...
- Qt Installer Framework翻译(7-7)
脚本API 下表总结了可以在控制器和组件脚本中使用的全局JavaScript对象. QMessageBox 提供一个模式对话框,通知用户或询问用户问题并接收答案 buttons 提供可在安装程序页面上 ...
- 5G三大应用场景
5G三大应用场景:eMBB(增强移动宽带).eMTC(海量物联).uRLLC(高可靠低时延连接) ------20191215闪
- openWRT和LuCI
openwrt是一套集成在板子上的系统,通过ip进入到其页面上 Luci是lua和UCI统一配置接口的合体,实现路由的网页配置界面(相当于一个前端框架)
- 一些java基础知识的备忘
接口和抽象类的区别是什么? 接口的方法默认是 public,所有方法在接口中不能有实现(Java 8 开始接口方法可以有默认实现),而抽象类可以有非抽象的方法. 接口中除了static.final变量 ...
- 01-SV入门及仿真环境搭建
1.SV入门 参考书籍<SystemVerilog验证 测试平台编写指南> [美]克里斯·斯皮尔 著 2.仿真环境搭建 仿真工具:modelsim se 2019.2,它不仅支持Veril ...
- 微信小程序——自定义菜单切换栏tabbar组件
效果图: wxml代码: <view class="top_tabbar" > <block wx:for="{{itemName}}" wx ...
- Jenkins +Ant +Jmeter(apache-jmeter-5.1.1)自动化性能测试平台
1.安装配置好Jdk, 下载网址:https://www.cr173.com/soft/33894.html 2.Jmeter下载地址:http://jmeter.apache.org/downloa ...
- Supervision meeting with Liu
data stream;streampipe/nifi data flow:1. algorithm;;;; top-k pattern, motif and so on 2. implication ...
- ApiBehaviorOptions 统一模型验证配置不生效
ApiBehaviorOptions 的统一模型验证配置一定要放到(.AddMvc)后面.