[英]How to add and ignore a field for json response
我正在使用RestEasy和hibernate在Jackson中返回響應。 我有一個bean播放器,其中包含字段:name,id,age,position。
現在,我正在實現兩個GET
rest方法來恢復json。
getPlayer()
,它返回一個玩家:名字,身份,年齡,位置。
getPlayers()
,它返回一個玩家列表,但有了這個玩家列表,我不想返回位置。
我的意思是,我如何為一個響應添加一個字段而忽略它以獲得另一個響應。
請建議。
謝謝
您應該在POJO getter上使用@JsonIgnore
注釋。
http://jackson.codehaus.org/1.0.1/javadoc/org/codehaus/jackson/annotate/JsonIgnore.html
更新:
您需要使用@JsonIgnoreProperties
接口,並在Request映射@JsonIgnoreProperties
其設置為@JSONFilter
。
您可以在此處閱讀更多相關信息: http : //www.jroller.com/RickHigh/entry/filtering_json_feeds_from_spring
我正在使用tomee,忽略json響應transient
一個字段為我工作,但我不知道它是否是正確的方法(我的應用程序沒有傑克遜可見,我只包括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");
}
}
賓語
public class MyObject {
public MyObject() {
}
public MyObject(String name, String color) {
this.name = name;
this.color = color;
}
public String name;
public transient String color;
}
響應
{"name":"myname"}
你不能簡單地取消位置字段嗎?
@GET
@Path("/players")
public List<Player> getPlayers(){
List<Player> players = getPlayersFromHibernate();
for(Player player : players)
player.setPosition(null);
return players;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.