RESTEasy + Jackson: how to exclude fields in a response?

I am porting my Java web application from a servlet to JAX-RS. Since I use Jboss, I will also use (default) RESTEasy.

In my servlets, I use Jackson to serialize / deserialize JSON; Jackson allows me to programmatically filter the inclusion / exclusion of fields, for example:

ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY); String[] ignorableFieldNames = { "id", "name" }; FilterProvider filters = new SimpleFilterProvider(). addFilter("f123",SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames)); mapper.filteredWriter(filters).writeValueAsString(object); 

RESTEasy provides Jackson support, but it seems to be transparently implemented by the developer, so I can’t switch to the low-level include / exclude fields. Is it possible?

+6
source share
1 answer

You can use Jackson annotations to declaratively customize almost everything. In your case, @JsonIgnore should be enough.

You can use JSON views if you do not want to ignore these fields all the time.

If you cannot change the code of the class in question (say, because it is in a third-party library), you have covered mix-ins.

And if you still find that you need to access ObjectMapper : Access Jackson's object mapper in RestEasy .

See also: http://wiki.fasterxml.com/JacksonAnnotations

+8
source

Source: https://habr.com/ru/post/977949/


All Articles