How to add and ignore json response field

I use RestEasy and sleep mode to return a response in Jackson. I have a bean Player with fields: name, identifier, age, position.

Now I am implementing two GET rest methods to remove json.

  • getPlayer() , which returns the player: name, identifier, age, position.

  • getPlayers() , which returns a list of players, but with this list of players I do not want to return a position.

I mean, how can I add a field for one answer and ignore it for another answer.

Please offer.

thanks

+5
source share
3 answers

Can't you just delete the position field?

 @GET @Path("/players") public List<Player> getPlayers(){ List<Player> players = getPlayersFromHibernate(); for(Player player : players) player.setPosition(null); return players; } 
-7
source

You must use the @JsonIgnore annotation for the POJO receiver.

http://jackson.codehaus.org/1.0.1/javadoc/org/codehaus/jackson/annotate/JsonIgnore.html

Update:

You need to use the interface with @JsonIgnoreProperties and set it as @JSONFilter in your request mapping.

You can read more about this here: http://www.jroller.com/RickHigh/entry/filtering_json_feeds_from_spring

+17
source

I use tomee to ignore the field in the json answer transient works for me, but I don’t know if it is the right way (there is no joson visible for my application, I just turned on jee -web api):

servlets

 import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/") @Produces({ MediaType.APPLICATION_JSON }) public class JsonApi { @GET @Path("testapi") public MyObject testApi() { return new MyObject("myname", "mycolor"); } } 

An object

 public class MyObject { public MyObject() { } public MyObject(String name, String color) { this.name = name; this.color = color; } public String name; public transient String color; } 

Answer

 {"name":"myname"} 
0
source

Source: https://habr.com/ru/post/977955/


All Articles