@Test public void testToXML() { String expectedXml = "<MSString>testing 123</MSString>"; MSString obj = new MSString("testing 123"); String xml = dtoUtil.toXML(obj); assertThat(xml).isEqualTo(expectedXml); }
@SuppressWarnings({ "unchecked", "known.nonnull" }) public <T> String toXML(@Nonnull T obj) { try { JAXBContext jc = JAXBContext.newInstance(obj.getClass()); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.setProperty(Marshaller.JAXB_FRAGMENT, true); StringWriter writer = new StringWriter(); if (obj.getClass().getAnnotation(XmlRootElement.class) != null) { m.marshal(obj, writer); } else { m.marshal(new JAXBElement<T>(new QName("", obj.getClass() .getSimpleName()), (Class<T>) obj.getClass(), obj), writer); } return writer.toString(); } catch (Exception e) { LOG.error("toXML failed", e); return obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode()); } }
DTOUtil { @SuppressWarnings({ "unchecked", "known.nonnull" }) public <T> String toXML(@Nonnull T obj) { try { JAXBContext jc = JAXBContext.newInstance(obj.getClass()); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.setProperty(Marshaller.JAXB_FRAGMENT, true); StringWriter writer = new StringWriter(); if (obj.getClass().getAnnotation(XmlRootElement.class) != null) { m.marshal(obj, writer); } else { m.marshal(new JAXBElement<T>(new QName("", obj.getClass() .getSimpleName()), (Class<T>) obj.getClass(), obj), writer); } return writer.toString(); } catch (Exception e) { LOG.error("toXML failed", e); return obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode()); } } }
DTOUtil { @SuppressWarnings({ "unchecked", "known.nonnull" }) public <T> String toXML(@Nonnull T obj) { try { JAXBContext jc = JAXBContext.newInstance(obj.getClass()); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.setProperty(Marshaller.JAXB_FRAGMENT, true); StringWriter writer = new StringWriter(); if (obj.getClass().getAnnotation(XmlRootElement.class) != null) { m.marshal(obj, writer); } else { m.marshal(new JAXBElement<T>(new QName("", obj.getClass() .getSimpleName()), (Class<T>) obj.getClass(), obj), writer); } return writer.toString(); } catch (Exception e) { LOG.error("toXML failed", e); return obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode()); } }  }
DTOUtil { @SuppressWarnings({ "unchecked", "known.nonnull" }) public <T> String toXML(@Nonnull T obj) { try { JAXBContext jc = JAXBContext.newInstance(obj.getClass()); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.setProperty(Marshaller.JAXB_FRAGMENT, true); StringWriter writer = new StringWriter(); if (obj.getClass().getAnnotation(XmlRootElement.class) != null) { m.marshal(obj, writer); } else { m.marshal(new JAXBElement<T>(new QName("", obj.getClass() .getSimpleName()), (Class<T>) obj.getClass(), obj), writer); } return writer.toString(); } catch (Exception e) { LOG.error("toXML failed", e); return obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode()); } }  @SuppressWarnings({ "unchecked", "known.nonnull" }) String toXML(@Nonnull T obj); T toObject(String xml, Class<T> clazz); String toJSON(Object obj); static T fromJSONToObject(String json, Class<T> clazz); }
DTOUtil { @SuppressWarnings({ "unchecked", "known.nonnull" }) public <T> String toXML(@Nonnull T obj) { try { JAXBContext jc = JAXBContext.newInstance(obj.getClass()); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.setProperty(Marshaller.JAXB_FRAGMENT, true); StringWriter writer = new StringWriter(); if (obj.getClass().getAnnotation(XmlRootElement.class) != null) { m.marshal(obj, writer); } else { m.marshal(new JAXBElement<T>(new QName("", obj.getClass() .getSimpleName()), (Class<T>) obj.getClass(), obj), writer); } return writer.toString(); } catch (Exception e) { LOG.error("toXML failed", e); return obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode()); } }  @SuppressWarnings({ "unchecked", "known.nonnull" }) String toXML(@Nonnull T obj); T toObject(String xml, Class<T> clazz); String toJSON(Object obj); static T fromJSONToObject(String json, Class<T> clazz);  }