简体   繁体   English

如何仅返回响应中的某些字段

[英]How to return only some fields in the response

I have made a service that returns an array of UserSettings objects:我做了一个返回 UserSettings 对象数组的服务:

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/")
public Response getBulkSettings(@QueryParam("fields") List<String> fields, @QueryParam("ids") List<String> ids) {
 List<UserSettings> responseArr = mailerService.fetchSettings(ids,fields);
return Response.ok(responseArr).build();
}

When I make a GET request on the URL http://localhost:8181/settings?ids=123&fields=customData,user_id I get the following:当我对 URL http://localhost:8181/settings?ids=123&fields=customData,user_id发出 GET 请求时我得到以下信息:

[
{
    "id": 0,
    "user_id": 123,
    "customData": "testCustomDataFor123",
    "deactivationDate": null
}
]

While what I want is :虽然我想要的是:

[
{
    "user_id": 123,
    "customData": "testCustomDataFor123"
}
]

@JsonIgnore放在您不想要的字段或其 getter 中。

Using the annotation @JsonIgnore is a solution if you can decide on the attributes to be filtered at compile-time.如果您可以决定要在编译时过滤的属性,则使用注释@JsonIgnore是一种解决方案。 In your example you want to filter at run-time, which can be achieved using techniques from your JSON library.在您的示例中,您希望在运行时进行过滤,这可以使用 JSON 库中的技术来实现。 For example, when using Genson you could do something like this:例如,在使用 Genson 时,您可以执行以下操作:

new GensonBuilder().exclude("password").create();

However, by doing so you loose the advantage of not having to care about how your response is serialised into JSON.但是,通过这样做,您失去了不必关心您的响应如何序列化为 JSON 的优势。 Therefore, I would like to suggest that you think if it is really necessary that clients can dynamically decide on the attributes to be returned.因此,我建议您考虑一下是否真的有必要让客户端动态决定要返回的属性。 Another solution might be to use media-types other than application/json that would allow the client to request different views of the resource.另一种解决方案可能是使用application/json以外的媒体类型,这将允许客户端请求资源的不同视图。 Jersey distributes incoming requests using the media-type given in the Accept header to the methods in the service class. Jersey 使用Accept标头中给定的媒体类型将传入请求分发给服务类中的方法。 In each method you can then work with different sub-classes of your UserSettings class that exclude different attributes using the annotation @JsonIgnore .在每种方法中,您都可以使用UserSettings类的不同子类,这些子类使用注释@JsonIgnore排除不同的属性。

You could do it how the other responses suggests.你可以按照其他回应的建议去做。 Another option with JAX-RS would be to leverage another Genson feature that enables you to filter what properties should be included or excluded. JAX-RS 的另一个选择是利用另一个 Genson 功能,该功能使您能够过滤应包含或排除的属性。 To do so register a custom Genson instance with this special Filter.为此,使用此特殊过滤器注册自定义 Genson 实例。

UrlQueryParamFilter filter = new UrlQueryParamFilter();
Genson genson = new GensonBuilder().useRuntimePropertyFilter(filter).create();
new ResourceConfig()
  .register(new GensonJaxRSFeature().use(genson))
  .register(filter);

And then in the query define the properties you want to include or exclude like that: http://localhost/foo/bar?filter=age&filter=name .然后在查询中定义要包含或排除的属性: http://localhost/foo/bar?filter=age&filter=name

Some more explanation can be found here .可以在此处找到更多解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM