RESTEasy + Jackson:如何在响应中排除字段? [英] RESTEasy + Jackson: how to exclude fields in the response?

查看:160
本文介绍了RESTEasy + Jackson:如何在响应中排除字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的Java Web应用程序从基于servlet的迁移到JAX-RS。因为我正在使用Jboss,所以我也会使用(默认情况下)RESTEasy。

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.

在我的servlet中,我使用Jackson序列化/反序列化JSON; Jackson允许我以编程方式过滤字段的包含/排除,例如:

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提供Jackson支持,但它似乎是透明地嵌入开发人员所以我不是能够进入低级别以包含/排除字段。这是可行的吗?

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?

推荐答案

你可以使用 Jackson注释以声明方式配置几乎所有内容。在你的情况下, @JsonIgnore 应该足够了。

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

你可以使用 JSON视图如果您不想一直忽略这些字段。

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

如果您无法修改相关类的代码(例如,因为它位于第三方库中) mix- ins已经让你了解。

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.

如果仍然发现你需要访问 ObjectMapper 在RestEasy中访问Jackson Object Mapper

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

另请参阅: http://wiki.fasterxml.com/JacksonAnnotations

这篇关于RESTEasy + Jackson:如何在响应中排除字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆