7

I'm using RestEasy and hibernate to return response in Jackson. I have a bean Player having fields: name, id, age, position.

Now, I'm implementing two GET rest methods for returing json.

  1. getPlayer(), which is returning a player: name, id, age, position.

  2. getPlayers(), which is returning a list of players, but with this list of players, i do not want to return position.

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

Please suggest.

Thanks

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Romi
  • 4,833
  • 28
  • 81
  • 113

3 Answers3

18

You should use @JsonIgnore annotation on the POJO getter.

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

Update:

You need to use interface with @JsonIgnoreProperties and set it as @JSONFilter on your Request mapping.

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

danny.lesnik
  • 18,479
  • 29
  • 135
  • 200
  • 1
    that i know, but i want same field to ignore for one reponse and same field to add for another – Romi Jan 17 '12 at 10:41
  • as given in the link... i need to make a interface AppDetailFilter(), and use @JsonFilter with the request. but am not getting what should be in interface, and how to use it to ignore diffrent fields for different request. – Romi Jan 17 '12 at 11:33
  • This is just kind of marker interface which has no methods. You can configure few interfaces. and then you map each request with this interface. – danny.lesnik Jan 17 '12 at 11:50
1

I am using tomee, to ignore a field in json response transient works for me, but i don't know if it is the correct way to go (there is no jackson visible to my application, i just included jee-web api):

Servlet

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");
    }
}

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;
}

Response

{"name":"myname"}
wutzebaer
  • 14,365
  • 19
  • 99
  • 170
  • This approach may introduce problems when using persistence, I wouldn't go that way. On the other hand, if you use this object just for data transfer, it should be fine. – holmicz Mar 04 '17 at 23:54
-8

Can't you simply null out the position field?

@GET
@Path("/players")
public List<Player> getPlayers(){
    List<Player> players = getPlayersFromHibernate();

    for(Player player : players)
        player.setPosition(null);

    return players;
}
eiden
  • 2,329
  • 19
  • 19
  • 4
    this opens you up to introducing a bug if you forget to write the code to null out the field. use `@JsonIgnore` so you don't have to. – John Gordon Jan 16 '13 at 19:38
  • How will that solve the the OP's requirement "How can I add a field for one response and ignore it for another response." ? Besides, 'forgetting' to do stuff when you code will always cause bugs ;) – eiden Jan 17 '13 at 10:39
  • 2
    This looks the simplest solution to me, along with adding @JsonIgnore to properties which have to be always ignored. Eiden, have you found any other way to do it? I do not like the way JsonView and JsonFilter can be used to get the same result. – Jayz Mar 04 '13 at 05:30
  • is this working? cuz I think that way too, and I made my unnecessary records null before I send it to the `ObjectMapper` to convert it to JSON. Mapper gives null pointer error. I think it is about sub fields of "product" that I set null. Is there a solution for this? – efirat Mar 14 '13 at 16:13
  • Of course this is more a workaround than an answer, but as a quick and dirty solution it works in case the JsonIgnore annotation is not available for some other reason (if you have compiled pojo for instance). – elbuild Oct 28 '16 at 13:37