參考:html
http://www.javashuo.com/article/p-odsxowpt-hh.html 去掉 xsi:type=
"echoBody"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
java
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;
注意spring
1:<?xml version="1.0" encoding="UTF-8"?> 的生成緩存
2.去掉 xsi:type=
"echoBody"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
工具
注意:因爲使用了緩存,緩存的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; } }