简体   繁体   English

如何过滤JAX-RS服务的响应?

[英]How to filter response of a JAX-RS service?

I got the following value object: 我得到以下值对象:

@XmlRootElement
public class Movie{
  public String name;
  public Date releaseDate;
  public List<Actors> actors;
}

and i got the following service 我得到了以下服务

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Movie> moviesByYear(int year){
//return all movies by year
}

The movies are searched in the database by some ORM framework. 电影是通过某些ORM框架在数据库中搜索的。 My question is: I want to filter the response, to not return the actors list (because this field is not relevant, and makes the response larger). 我的问题是:我想过滤响应,不返回参与者列表(因为此字段不相关,并且使响应变大)。 Of course I can 当然,我可以

for(Movie movie: movies){
  movie.actors = null;
}

but this will escalate quickly if I want to remove multiple fields. 但这会迅速升级,如果我想删除多个字段。

If you never want to include the actors field in your response, you can annotate the field with @XmlTransient . 如果您不想在响应中包括actors字段,则可以使用@XmlTransient注释该字段。 See the JavaDoc for more details. 有关更多详细信息,请参见JavaDoc

Otherwise, you could wrap the Movie object into a wrapper object that doesn't expose the actors field. 否则,您可以将Movie对象包装到不公开actors字段的包装对象中。

如果服务没有执行此操作的方法,则必须更改服务代码本身。

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

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