简体   繁体   English

如何获取要序列化并发送到客户端的Java对象字段

[英]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.

相关问题 RMI:远程对象的字段是否已序列化并发送到客户端? - RMI: are fields of remote object serialized and sent to client? 序列化对象通过UDP发送到Java中的客户端后具有空值 - Serialized object has null values after being sent via UDP to client in java 如何获取表示Java对象的序列化字节数? - How to get amount of serialized bytes representing a Java object? 在Java中读取序列化对象时获取EOFException - Get EOFException while reading serialized object in Java 如何使通过网络传递的Java序列化对象将更改通知客户端GUI - How to make a Java serialized object being passed over network notify client GUI of change 将序列化对象从客户端传递到Spring控制器并返回序列化对象响应的最简单方法? - Simplest way to pass a Serialized object from client to Spring controller and get back serialized object response? 如何在Java中保留对序列化对象的引用? - How to keep Reference to a Serialized object in Java? 如何将序列化对象传递给appengine java任务? - How to pass a serialized object to appengine java task? 我如何查看java序列化对象? - How do I peek in a java serialized object? 如何在PHP中反序列化用Java序列化的对象 - How to deserialize in PHP an object serialized in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM