With RESTEasy and Jackson, is it possible to use the @RolesAllowed
annotation in my model, in order to avoid certain properties to be serialized in output, depending on the role of the user?
I already found a ton of documentation on how to do this with Jersey, but nothing with RESTEasy.
I'm blocked on this architecture so switching libraries is not an option, and using the custom ObjectMapper
as explained here is not an option either, as the model is big enough to make it too time-consuming to mark every single property of a large dataset for correct serialization. Plus, this refers to an older version of the Jackson library and I'm not sure on how to make it work with the new version.
EDIT
Specifically see this blog post to understand what I'm trying to accomplish. Please note that this is Jersey-specific and so far I found no documentation on RESTEasy to accomplish this.
If you are not willing to use
@JsonView
, you could consider@JsonFilter
. You first need to extendSimpleBeanPropertyFilter
and control the serialization according to the user roles:To apply the filter to a certain bean, annotate it with
@JsonFilter("roleBasedPropertyFilter")
:Then register your filter in your the
ContextResolver
forObjectMapper
:If you want to make your filter "global", that is, to be applied to all beans, you can create a mix-in class and annotate it with
@JsonFilter("roleBasedPropertyFilter")
:Then bind the mix-in class to
Object
: