Search code examples
javajsonjacksonrpcjson-rpc

Json RPC Custom Object is null after getting it over network


I'm trying to build two programs that can interact with each other over network using JSON RPC.

It currently works fine for primitive types like integers, but Custom Classes/Objects get always returned as null.

This is the method that fetches the data from the database. The data gets collected correctly and no field is null.

public LinkedList<ServiceInformation> getServiceInformations() {
    LinkedList<ServiceInformation> returnList = new LinkedList<>();

    try (Connection conn = DriverManager.getConnection("jdbc:h2:" + Main.config_db_location + ";AUTO_SERVER=TRUE",
            Main.config_db_username, Main.config_db_password)) {
        // read data from database
        PreparedStatement stmt = conn.prepareStatement("SELECT * FROM BCSTASKS_SERVICE");

        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            // Create a new ServiceInformation with the name and description
            ServiceInformation tempInformation = new ServiceInformation(rs.getString("TicketName"),
                    rs.getString("TicketDescription"), rs.getString("JTaskID"), rs.getBoolean("wasShown"));
            // Add the ServiceInformation to the list
            returnList.add(tempInformation);
        }

        stmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    
    return returnList;
}

This is the method that tries to call the other program and returns a List of Custom Objects. After getting the data over network all the fields are null.

public static LinkedList<ServiceInformation> getServiceInformationsRPC() {
    LinkedList<ServiceInformation> returnList = new LinkedList<>();
    
    try {
        URL url = new URL(getAPIUrl());

        JsonRpcHttpClient client = new JsonRpcHttpClient(url);
        
        DatabaseService dbService = ProxyUtil.createClientProxy(Syncer.class.getClassLoader(), DatabaseService.class, client);
        
        returnList = dbService.getServiceInformations();

        return returnList;
    } catch (Throwable t) {
        Main.logger.error("Couldn't get service information");
        t.printStackTrace();
    }
    return returnList;
}

I don't think there is something wrong with the methods and my guess is that I'm missing something in the object class itself, because Jackson tries to deserialize the object and I'm missing an annotation or something.

@JsonIgnoreProperties(ignoreUnknown = true)
public class ServiceInformation {
/** The name of the ticket */
@JsonProperty("TicketName")
private final String TicketName;

/** The description of the ticket */
@JsonProperty("TicketDescription")
private final String TicketDescription;

/** The JTaskID of the ticket */
@JsonProperty("JTaskID")
private final String JTaskID;

/** Boolean to see if the ticket was already shown as a notification */
@JsonProperty("WasShown")
private boolean WasShown;

/**
 * Class that defines a ServiceInformation/Ticket.
 * 
 * @param TicketName        is the name of the ticket.
 * @param TicketDescription is a more detailed description of the problem.
 * @param JTaskID
 * @param WasShown
 */
@JsonCreator
public ServiceInformation(@JsonProperty("TicketName") String TicketName,
        @JsonProperty("TicketDescription") String TicketDescription, @JsonProperty("JTaskID") String JTaskID,
        @JsonProperty("WasShown") boolean WasShown) {
    this.TicketName = TicketName;
    this.TicketDescription = TicketDescription;
    this.WasShown = WasShown;
    this.JTaskID = JTaskID;
}

/**
 * Get the ticket name.
 * 
 * @return {@link #TicketName}
 */
@JsonProperty("TicketName")
public String getTicketName() {
    return TicketName;
}

/**
 * Get the ticket description.
 * 
 * @return {@link #TicketDescription}
 */
@JsonProperty("TicketDescription")
public String getDescription() {
    return TicketDescription;
}

/**
 * Get the tickets JTaskID
 * 
 * @return {@link #JTaskID}
 */
@JsonProperty("JTaskID")
public String getJTaskID() {
    return JTaskID;
}

/**
 * Get the value of {@code WasShown}
 * 
 * @return {@link #WasShown}
 */
@JsonProperty("WasShown")
public boolean getWasShown() {
    return WasShown;
}
}

Both programs use the same ServiceInformation class which is located in a seperate program and installed as a dependency in both the programs.

My question is if I'm missing something in the ServiceInformation class, because before I transfer it over network all the fields have values and after transfering all fields are null.


Solution

  • After trying to figure out a solution I changed every LinkedList into a normal List. This seemed to solve the problem.

    For whatever reason Java couldn't serialize a LinkedList.