ps0604
ps0604

Reputation: 1081

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

I'm migrating my Java web application from servlet-based to JAX-RS. Since I'm using Jboss I'll also use (by default) RESTEasy.

In my servlets I use Jackson to serialize/deserialize JSON; Jackson allows me to filter programatically 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 that it is embedded transparently to the developer so I'm not able to get to the low-level to include/exclude fields. Is this feasible?

Upvotes: 11

Views: 17139

Answers (1)

Matt Ball
Matt Ball

Reputation: 359986

You can use Jackson annotations to declaratively configure pretty much everything. In your case, @JsonIgnore should suffice.

You can use JSON views if you don't want to ignore those fields all the time.

If you can't modify the code of the class in question (say, because it's in a third-party library) mix-ins have you covered.

And if you still find that you need to access the ObjectMapper: Accessing Jackson Object Mapper in RestEasy.

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

Upvotes: 14

Related Questions