I\'m trying to consume a web service exposed by a provider. The Provider has a strict checking at his end that the request xml should not contain tags which don\'t have valu
This worked for me, when serializing a class where I couldn't use annotations (LogRecord
):
JAXBContext jc = JAXBContext.newInstance(LogRecord.class);
JAXBElement je = new JAXBElement<>(new QName("log"), LogRecord.class, record);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setAdapter(new XmlAdapter() {
@Override
public String marshal(String v) throws Exception {
if(null == v || v.length() == 0) {
return null;
}
return v;
}
@Override
public String unmarshal(String v) throws Exception {
return v;
}
});
ByteArrayOutputStream xml = new ByteArrayOutputStream();
marshaller.marshal(je, xml);