[英]How to get Java object field to be serialized and sent to the client
I have an AngularJS application using Ebean and Play Framework. 我有一个使用Ebean和Play框架的AngularJS应用程序。 I'm using Java 7. I have a Java object on the server that is supposed to be sent to the client after being fetched from the database.
我正在使用Java7。服务器上有一个Java对象,应该从数据库中提取该对象后再发送给客户端。 It is a RESTful service and objects are sent in Json.
这是一个RESTful服务,对象在Json中发送。 I added a new field to this object, it is itself an object.
我向该对象添加了一个新字段,它本身就是一个对象。 I expect this field to be serialized and sent to the client but it is not being sent.
我希望此字段被序列化并发送到客户端,但不会被发送。 Is there any annotation or anything else I can do to get the field sent to the client?
是否有任何注释或我可以做的其他任何事情来将该字段发送给客户端? The field I want sent is call service, and it is a Resource object.
我要发送的字段是呼叫服务,它是一个Resource对象。
public Class DVMOrderItem extends OrderItem {
private static final long serialVersionUID = -2757166118137807642L;
@OneToOne(mappedBy = "discoOrderItem")
protected Resource service;
/** getters and setters available */
You should try to create a serializer for this field 您应该尝试为此字段创建序列化程序
First add an annotation above the field : 首先在字段上方添加注释:
@JsonSerialize(using=MyJsonSerializer.class)
Then create your json serializer class : 然后创建您的json序列化器类:
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
public class MyJsonSerializer extends JsonSerializer<Customer>
{
@Override
public void serialize(Customer comp, JsonGenerator gen, SerializerProvider provider)throws IOException, JsonProcessingException
{
//This is how to create a json object with jackson
gen.writeStartObject();
gen.writeObjectField("idCustomer", myValue1);
gen.writeObjectField("name", myValue2);
gen.writeEndObject();
}
}
The MyJsonSerializer class will be call every time you want to serialize the field. 每当您要序列化字段时,都会调用MyJsonSerializer类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.