[英]How can I only return some fields in the JSON object?
I currently have a function that returns a JSON version of an object: 我目前有一个返回对象的JSON版本的函数:
public class Debate extends Controller
{
public static Result viewArgument(Long id)
{
...
return ok(Json.toJson(Argument.get(id)));
}
}
This Argument
object has some confidential information that should not be exposed to the client, however. 但是,此
Argument
对象具有一些不应向客户端公开的机密信息。 How can I select only the fields id
and summary
to be returned in the resulting JSON object? 如何仅选择要在结果JSON对象中返回的字段
id
和summary
?
You can copy the Argument
's id
and summary
into a DTO (Data Transfer Object). 您可以将
Argument
的id
和summary
复制到DTO(数据传输对象)中。 Then turn that into JSON to be sent over the wire. 然后将其转换为JSON,以通过网络发送。
public class ArgumentDto {
public Long id;
public String summary;
}
public class Debate extends Controller
{
public static Result viewArgument(Long id)
{
...
Argument originalArgument = Argument.get(id);
ArgumentDto argument = new ArgumentDto();
dto.id = originalArgument.id;
dto.summary = originalArgument.summary;
return ok(Json.toJson(dto));
}
}
有一个更简单的答案,只需将@JsonIgnore添加到其他字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.