参考:

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;
    }
}