参考:

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的更多相关文章

  1. jaxb

    一.简介 JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实 ...

  2. JavaEE学习之JAXB

    一.前言 JAXB——Java Architecture for XML Binding,是一项可以根据XML Schema产生Java类的技术.JAXB提供将XML实例文档反向生成Java对象树的方 ...

  3. XmlRootElement JAXB

    http://desert3.iteye.com/blog/1570092(文章已经很好) 看了那边文章以后尝试后写点直白的 PROPERTY: JAXB 绑定类中的每个获取方法/设置方法对将会自动绑 ...

  4. 错误: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 ...

  5. Jaxb annotation使用

    JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例文档反向 ...

  6. JAXB最佳实践

    JAXB主要用来实现对象和XML之间的序列化和反序列化. 本文主要总结JAXB基本使用方法和注意事项! 通过下文的XML示例内容进行JAXB的简单实践 <?xml version="1 ...

  7. 在Gradle中使用jaxb的xjc插件

    jaxb,全称为Java Architecture for Xml Binding,是一种将java对象与xml建立起映射的技术.其主要提供两个功能,一是将java对象映射为xml,二是将xml映射为 ...

  8. Java for XML: JAXP、JAXB、JAXM、JAX-RPC、JAX-WS

    在XML领域里,对XML文件的校验有两种方式:DTD校验.Schema校验.在Java中,对于XML的解析,有多种方式:DOM解析.SAX解析.StAX解析.结合XML和Java后,就产生了Bind技 ...

  9. XStream、JAXB 日期(Date)、数字(Number)格式化输出xml

    XStream.Jaxb是java中用于对象xml序列化/反序列化 的经典开源项目,利用它们将对象转换成xml时,经常会遇到日期(Date).数字按指定格式输出的需求,下面是使用示例: 一.日期字段格 ...

  10. java JAXB 学习

    JAXB(Java Architecture for XML Binding)是JDK的一部分,用于Object <-> XML的转换(有点类似于.NET中的XML序列化). 1.创建XS ...

随机推荐

  1. VsCode开发Java SpringBoot遇到的问题

    报错截图 报错一:Build failed, Do you want to continue? 编译失败,你想继续吗? 报错二:ConfigError:The Project "Demo&q ...

  2. C++-蓝桥杯-矩阵乘法[快速幂]

    忘了改矩阵的大小居然还有33分,我醉了 #include <cstdio> ; struct Matrix{int a[N][N];}; int n,m; Matrix A,O,I; Ma ...

  3. MPI Maelstrom POJ - 1502 floyd

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> usin ...

  4. SVM-支持向量机(二)非线性SVM分类

    非线性SVM分类 尽管SVM分类器非常高效,并且在很多场景下都非常实用.但是很多数据集并不是可以线性可分的.一个处理非线性数据集的方法是增加更多的特征,例如多项式特征.在某些情况下,这样可以让数据集变 ...

  5. [国家集训队] 拉拉队排练 - Manacher

    用 Manacher 跑出回文串长,注意这里不需要偶数长度所以不需要对串做一些奇怪的处理 然后用前缀和搞一下,计算答案时跑快速幂即可 #include <bits/stdc++.h> us ...

  6. vscode设置成中文

    打开 VS Code Ctrl + Shift +p打开搜索框 搜索框内输入Configure Display Language 回车 修改代码中“locale”后面引号内内容为zh-CH 重新启动V ...

  7. 数组的concat连接

    let arr1 = [1,3,5],arr2 = [2,32,78],arr3 = [];arr3 = arr1.concat(arr2);// arr1 = [1,3,5] arr2 = [2,3 ...

  8. Go生成随机数

    生成随机数 概念 伪随机数,都是根据一定的算法公式算出来的. 所在包 math/rand 生成随机数的公式需要一个种子数,一般为整数.种子数相同会导致每次启动程序是生成随机数相同,为了避免重复每次生成 ...

  9. ubuntu18+caffe+cuda

    昨天安装caffe,因为用的是cuda10.2,遇到各种问题,最终也没有安装成功.使用cmake配置成功.生成成功.编译的时候报错. /usr/local/cuda/include/cuda_runt ...

  10. sql查询 —— 分组

    -- 分组 -- group by -- 分组只有与聚合函数一起使用才能发挥作用 -- 分组只限于字段分明 例如 性别 ,部门, --列出所有性别 select gender from student ...