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

杀马特。学长 韩版系。学妹 提交于 2019-12-05 23:48:03

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/26831876/resteasy-jackson-how-to-exclude-fields-in-the-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!