测试

    @org.junit.Test
    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_ENCODING, "UTF-8");
        StringWriter stringWriter=new StringWriter();
        marshaller.marshal(model, stringWriter);
        String result=stringWriter.toString();
        System.out.println(result);
    }
@Request("OrderService")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "LogisticsOrderListModel")
@Data
public class LogisticsOrderListModel {

工具类 

1. spring中提供的convert   Jaxb2RootElementHttpMessageConverter

2.自定义的工具类

package com.sf.wms.sao.util;

import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

import javax.xml.bind.*;
import java.io.StringWriter;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author 01384526
 * @title: JAXBUtil
 * @projectName wms
 * @description: 参考 Jaxb2RootElementHttpMessageConverter,使用缓存
 * @date 2019-10-1515:36
 */
public class JAXBUtil {

    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");
            StringWriter result=new StringWriter();
            marshaller.marshal(o, result);
            return result.toString();
        }
        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, jaxbContext);
            }
            catch (JAXBException ex) {
                throw new HttpMessageConversionException(
                        "Could not instantiate JAXBContext for class [" + clazz + "]: " + ex.getMessage(), ex);
            }
        }
        return jaxbContext;
    }
}
01-22 08:19