Search code examples
javarestjaxbcxfjettison

JAXB: How to exclude specific field from marshaling in runtime?


I'm using JAXB+Jettison to serve multiformat RESTful (schema-less) service. @XmlTransient annotation served good for those properties that I don't want to serialize at all. But that's not enough.

How can I exclude specific field from marshaling in runtime?

Can I somehow preinitialize Marshaling context and define what fields to exclude?

I need that to selectively serialize object fields based on current user's role.


Solution

  • I've done similar before by having a number of DTOs in a hierarchy, from lean to full fat, each with differing JAXB mappings. However this is only practical for a small number of roles.

    MOXy does allow runtime binding alterations, seems to fit the bill for your use-case.

    However, beware with tampering with your service contracts, it can lead to weird live bugs that are a PAIN to track down.