1

我正在尝试将带有对象的消息从服务器发送到客户端套接字事件,但是当我在客户端收到消息时,结果中没有显示所有具有空值的键。顺便说一句,我NettySocketIO在服务器端使用。

服务器端:

@Override
@Scheduled(fixedDelay = 5000, initialDelay = 5000)
public void scheduledFindAllOrders() {
   SocketMessage message = new SocketMessage();
   List<Order> orders = this.repository.findAll();
   
   message.setSender("SYSTEM");
   message.setReceiver("ALL");
   message.setMessage("Lis of orders.");
   message.setData(orders);

   //send to all clients
   this.server.getBroadcastOperations().sendEvent("orders", message);
}

@Data
@Entity
@Table(name = TABLES.ORDER)
public class Order {
   
   @Id
   private Integer id;

   private Integer transporterId;
......other keys

客户端:

//socket-io v 2.2.0 
const socket = io('http://localhost:8080', {
    transports: ['polling', 'websocket']
});

socket.on('connect', function () {
    console.log('Connected');
});

socket.on('orders', function (data) {
    console.log('Received message', data);
});

我期待得到这样的东西

[{id: 1, transporterId: 2}, {id:2, transporterId: null}]

相反,我收到了这个

[{id: 1, transporterId: 2}, {id:2}]

SocketIO 客户端是否默认剥离所有具有空值的对象键?如果是这样,有什么方法可以解决这个问题吗?

4

1 回答 1

0
conf.setJsonSupport(new JacksonJsonSupport() {
    {
                      super.objectMapper.setSerializationInclusion(JsonInclude.Include.USE_DEFAULTS);

    }
   });

这里 conf 是 Web 套接字的配置

于 2021-07-13T07:49:55.443 回答