简体   繁体   中英

Exclude fields in Jersey2 Response

Im developing Jersey2 RESTful Service

Jersey2 v2.22.2 + Spring3 v3.2.16 + Jax-rs v2.0.1

@Path("/v1/games")
public class GameServiceV1 implements IGameServiceV1 {

    @Autowired
    GameDAO gameDao;

    @Autowired
    CompanyDAO companyDao;

    @Autowired
    DMapper mapper;

    @Override
    @GET
    @Produces({ MediaType.APPLICATION_JSON })
    public List<GameDTO> getGamesByCity(@QueryParam("city") String cityID, @QueryParam("template") String template)
            throws EscapeitorException {

        List<Company> companies = companyDao.getCompaniesByCityId(cityID);
        List<GameDTO> games = putCompaniesWithinGames(companies);
        return games;
    }

If I receive @QueryParam template = "summary", I need exclude some GameDTO response fields.

For example:

GET /rest/v1/games?city=1 must be return:

[  
  {  
     "id":"1",
     "name":"Zombie Lab",
     "company":"2",
     "description":"Laboratorio zombie des",
     "story":"historia",
     "players":"2-5",
     "length":60,
     "mode":"Team",
     "price":15.0,
     "web":"http://zombielab.com/",
     "image":null,
  }]

However,

GET /rest/v1/games?city=1? template=summary must be return:

[  
  {  
     "id":"1",
     "name":"Zombie Lab",
     "description":"Laboratorio zombie des",
     "web":"http://zombielab.com/",
  }]

How can I exclude fields?

You might want to take a look at projections in Spring Data REST. Basically this allows you define subsets (projections) of your DAO/DTO fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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