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 ...
随机推荐
- canvas特效-文字粒子
具体的效果 https://www.ui.cn/detail/393461.html 实现类似的动效 要求: 1.文字内容可以自定义 2.粒子的运动轨迹 是曲线,返回是按照 原运动轨迹 3.粒子 堆叠 ...
- npm 基础命令
npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准.有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包.npm 从5.2版开始,增加了 ...
- H3C 静态路由
一.静态路由简介 静态路由是一种特殊的路由,由管理员手工配置.当网络结构比较简单时,只需配置静态路由就可以使网络正常工作. 静态路由不能自动适应网络拓扑结构的变化.当网络发生故障或者拓扑发生变化后,必 ...
- PP: Multilevel wavelet decomposition network for interpretable time series analysis
Problem: the important frequency information is lack of effective modelling. ?? what is frequency in ...
- nginx 启动报错找不到nginx.pid文件
这个问题的出现应该是系统找不到nginx的配置文件nginx.conf,所以,我们要告诉系统配置文件的位置:' --- 使用nginx -c /usr/local/nginx/conf/nginx.c ...
- HCTF2018-admin[Unicode欺骗]
看源码发现 在修改密码,登录,注册时都有都用strlower()来转小写 看了网上师傅的wp,经验之谈,python中自带转小写函数lower(),但这里使用strlower(),可能存在猫腻. 跟进 ...
- javascript闭包的理解和实例
所谓闭包,值得是词法表示包括不必要计算的变量的函数,也就是说,该函数可以使用函数外定义的变量. 顺便提示一下: 词法作用域:变量的作用域是在定义时决定而不是执行时决定,也就是说词法作用域取决于源码,通 ...
- @Cacheable注解不生效原因
因为@Cacheable注解应用了AOP动态代理,生成代理类,判断缓存中是否存在该key,如果不存在则调用被代理类的标有@Cachable注解的方法,否则不执行. 所以当类A的方法a调用方法b(标有@ ...
- react 实现圆环进度条
import React, { useState, useEffect } from "react" import { css } from "emotion" ...
- BUUCTF [SUCTF 2019]EasySQL
首先打开网址 发现有三种显示方法 还有一个没有输出 可以堆叠注入 1;show databases; 1;show tables; 可以看到有一个Flag表 测试发现from flag都被过滤不能直接 ...