jackson Json RPC自定义对象在通过网络获取后为空

yhived7q  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(112)

我试图建立两个程序,可以相互作用,通过网络使用JSON RPC
它目前对整数之类的基元类型工作得很好,但自定义类/对象总是返回为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;
}

这是尝试呼叫其他程式并传回自订对象清单的方法。透过网络取得数据后,所有字段均为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;
}

我不认为这些方法有什么问题,我的猜测是我在对象类本身中遗漏了一些东西,因为Jackson试图反序列化对象,而我遗漏了一个注解或其他东西。

@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;
}
}

这两个程序都使用相同的ServiceInformation类,该类位于单独的程序中,并作为依赖项安装在这两个程序中。
我的问题是,我是否遗漏了ServiceInformation类中的某些内容,因为在我通过网络传输它之前,所有字段都有值,而在传输之后,所有字段都为空。

khbbv19g

khbbv19g1#

在试图找出解决方案后,我把每一个LinkedIn列表都改成了一个普通的列表。这似乎解决了问题。
不管出于什么原因,Java都不能序列化LinkedList。

相关问题