I’m creating a webservice using Apache CXF. In my webservice methods I need to keep some legacy logic, meaning that for some fields and read operations, if value retrieved from DB is 0, the corresponding field should not be present in outbound XML (and other way around – for write operations, if field is missing in inbound XML, the value stored in DB should be 0).
Previous version of my WS methods used AttributeConverter in entities to implement this logic, but I can’t use this approach anymore. So I tried to use XmlJavaTypeAdapter instead.
I created and applied the following adapter:
@Override
public Long unmarshal(String val) throws Exception {
if (val == null) {
return 0L;
}
return Long.parseLong(val);
}
@Override
public String marshal(Long val) throws Exception {
if (val == null || Long.valueOf(0).equals(val)) {
return null;
}
return val.toString();
}
The problem is that with this approach field is still present in resulting XML (for read operation), just as empty tag. I need to get rid of the tag at all (which is the case for other null fields for which I don’t use adapters).
How can I achieve this?
My DTO field is annotated with
@XmlElement(required = false, nillable = false)
(which should be fine, but I also checked other configuration of required and nillable params).
I verified that adapter is called correctly.
I would like to stick to adapter approach which seems most clear and concise (I know that I could for example put some logic in getters/setters for fields or do manual mapping elsewhere, but I would like to avoid that). I also read about approach with configuring custom marshaller listener, but this seems to cumbersome to me.
Is there a way to configure marshaller so if a value field is null (even as a result of adapter call), the field is completly skipped in resulting XML?