2

I'm trying to send a POJO over an RSocket requestStream:

import java.io.Serializable;

class GreetingRequest implements Serializable {
    private String name;

    public GreetingRequest() {}

    public GreetingRequest(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

If I were to send a String I can do:

ByteBuf data = ByteBufAllocator.DEFAULT.buffer().writeBytes("Hello".getBytes());

socket.requestStream(DefaultPayload.create(data, metadata))
        .map(Payload::getDataUtf8)
        .toIterable()
        .forEach(System.out::println);

But how can I serialise my POJO?

This is my attempt using implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0' which doesn't work:

GreetingRequest pojo = new GreetingRequest("Davide");
ByteBuf data = SerializationUtils.serialize(pojo);

socket.requestStream(DefaultPayload.create(data, metadata))
        .map(Payload::getDataUtf8)
        .toIterable()
        .forEach(System.out::println);
Javide
  • 2,477
  • 5
  • 45
  • 61

3 Answers3

2

There is a native java serialization mechanism that I would NOT recommend, but you can read about it. Read about Serialazable interface in Java API. There are 2 options that I would recommend:

  1. JSON-JACKSON (also known as Faster XML)
  2. GSON (mentioned in the answer from César Ferreira)

Both convert classes to JSON and vise-versa. For JSON-JACKSON see class ObectMapper. In particular methods writeValueAsString() or writeValueAsBytes() to serialize your object to JSON string or bytes. And to convert it back look for method readValue().

Here are the Maven artifacts that you would need to use it:

        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.12.3</version>        
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.12.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
      <version>2.12.3</version>
    </dependency>
Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
1

I recommend you using Gson converter. It helps you to convert a Java Class to a JSON String. And then you can work with the String as if you were working with simple text.

You can import the dependency:

dependencies {
  implementation 'com.google.code.gson:gson:2.8.7'
}

And then, can use jsonschema2pojo to convert the JSON:

{ "name": "Test" }

to classes like this:

    package com.example;
    
    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class GreetingRequest {
    
    @SerializedName("name")
    @Expose
    private String name;
    
    public String getName() {
    return name;
    }
    
    public void setName(String name) {
    this.name = name;
    }
    
    }

After all is done you can do something like this in Java:

    Gson converter = new Gson();
    GreetingsRequest request = new GreetingRequest();
    request.setName("Test");
    String greetingsJSON = converter.toJson(request);

And then you can still send the JSON string as it follows:

ByteBuf data = ByteBufAllocator.DEFAULT.buffer().writeBytes(greetingsJSON.getBytes());

socket.requestStream(DefaultPayload.create(data, metadata))
        .map(Payload::getDataUtf8)
        .toIterable()
        .forEach(System.out::println);

Data conversions:

  • JSON Object - Java class
  • Array - List<>

Helpful links:

César Ferreira
  • 681
  • 1
  • 5
  • 12
0

If you use a framework like Spring Boot this is taken care of for you. You may want to manually control in which case the other examples are more relevant, but there are productivity benefits to Spring Boot or rsocket-rpc.

https://github.com/rsocket/rsocket-demo/blob/master/src/main/kotlin/io/rsocket/demo/chat/ChatController.kt

https://spring.io/blog/2020/03/02/getting-started-with-rsocket-spring-boot-server

or rsocket-rpc-java using protobuf instead of Serialization

https://github.com/rsocket/rsocket-rpc-java/blob/master/docs/get-started.md

Yuri Schimke
  • 12,435
  • 3
  • 35
  • 69