jsonrpc4j null on JSON serialization
I'm getting a null error from jsonrpc4j
in the class CleengAPI.java
below from which the method rpc.listCustomers(publisherToken, offset, limit);
is called to serialize the following customer JSON list which is downloaded correctly by jsonrpc4j
and displayed in the debug statement, but the JSON is not serialized effectively resulting in the null error.
Customer list JSON
{
"result": [
{
"email": "",
"firstName": "xx",
"lastName": "xx",
"dateOfBirth": "",
"country": "US",
"companyName": "",
"phoneNumber": "",
"addressLine1": null,
"addressLine2": null,
"city": null,
"state": null,
"postalCode": null,
"regDate": "2019-12-24 07:06:46",
"lastLoginDate": "2019-12-24 07:06:46",
"transactions": "",
"payment": "",
"termsAccepted": "no",
"marketingOptIn": "no",
"lastUserIp": "xx.xx.x.x"
},
{
"email": "11@gmail.com",
"firstName": "xx",
"lastName": "xx",
"dateOfBirth": "",
"country": "US",
"companyName": "",
"phoneNumber": "",
"addressLine1": null,
"addressLine2": null,
"city": null,
"state": null,
"postalCode": null,
"regDate": "2019-02-12 02:10:57",
"lastLoginDate": "2019-03-19 17:10:28",
"transactions": "1",
"payment": "visa",
"termsAccepted": "no",
"marketingOptIn": "yes",
"lastUserIp": "xxx.xxx.xx.x"
}
],
"id": "854190206",
"error": null,
"jsonrpc": "2.0"
}
Class CleengAPI
import java.net.MalformedURLException;
import java.net.URL;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import com.googlecode.jsonrpc4j.JsonRpcParam;
import com.googlecode.jsonrpc4j.ProxyUtil;
import com.jahtoe.getner.domain.AccessStatus;
import com.jahtoe.getner.domain.Items;
import com.jahtoe.getner.domain.ListCustomers;
import com.jahtoe.getner.domain.ListCustomerSubscription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CleengApi {
private CleengRpc rpc;
public static final Logger LOG = LoggerFactory.getLogger(CleengApi.class);
private interface CleengRpc {
Items<ListCustomers> listCustomers(
@JsonRpcParam(value = "publisherToken") String publisherToken,
@JsonRpcParam(value = "offset") int offset,
@JsonRpcParam(value = "limit") int limit);
}
public CleengApi() throws MalformedURLException {
makeRpc("https://api.cleeng.com/3.0/json-rpc");
}
private void makeRpc(String apiUrl) throws MalformedURLException {
JsonRpcHttpClient client = new JsonRpcHttpClient(new URL(apiUrl));
rpc = ProxyUtil.createClientProxy(getClass().getClassLoader(), CleengRpc.class, client);
}
public Items<ListCustomers> ListCustomer(String publisherToken, int offset, int limit) {
return rpc.listCustomers(publisherToken, offset, limit);
}
}
Class ListCustomers to serialize JSON
import java.io.Serializable;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(include=JsonSerialize.Inclusion.NON_EMPTY)
public class ListCustomers implements Serializable {
private static final long serialVersionUID = 1L;
public String email;
public String firstName;
public String lastName;
public String dateOfBirth;
public String country;
public String companyName;
public String phoneNumber;
public Object addressLine1;
public Object addressLine2;
public Object city;
public Object state;
public Object postalCode;
public String regDate;
public String lastLoginDate;
public String transactions;
public String payment;
public String termsAccepted;
public String marketingOptIn;
public String lastUserIp;
}
Code nippet to create CleanAPI and execute ListCustomers download and serialization
CleengApi api = new
allListCustomer = api.ListCustomer(publisherToken, 0, 50);
POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jahtoe.XX</groupId>
<artifactId>XX</artifactId>
<version>1</version>
<packaging>jar</packaging>
<name>GregGetner</name>
<description>ETL: Cleeng to Zapier</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.github.briandilley.jsonrpc4j</groupId>
<artifactId>jsonrpc4j</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
Items Class
package com.cleeng.apiv3.domain;
import java.io.Serializable;
public class Items<X> implements Serializable {
private static final long serialVersionUID = 1L;
public int totalItemCount;
public java.util.Vector<X> items;
}
I'm not sure how to debug this as the message is simply the one word null
Any help would be appreciated.
Regards Conteh
1 ответ
Итак, что произошло: следующий стиль JSON, возвращаемый API, требует класса Items
{
"result": {
"items": [{
"offerId": "S321834685_US",
"status": "active",
"expiresAt": 15869957,
"nextPaymentPrice": 79,
"nextPaymentCurrency": "USD",
"paymentGateway": "adyen",
"paymentMethod": "visa"
}],
"totalItemCount": 1
},
"id": "299789347",
"error": null,
"jsonrpc": "2.0"
}
Однако для этого стиля требуется List. Я использовал Items с этим стилем, который вызывает null.
{
"result": [
{
"email": "",
"firstName": "vvv",
"lastName": "wwwww",
"dateOfBirth": "",
"country": "US",
"companyName": "",
"phoneNumber": "",
"addressLine1": null,
"addressLine2": null,
"city": null,
"state": null,
"postalCode": null,
"regDate": "2019-12-24 07:06:46",
"lastLoginDate": "2019-12-24 07:06:46",
"transactions": "",
"payment": "",
"termsAccepted": "no",
"marketingOptIn": "no",
"lastUserIp": "xx.xxx.xxx.xxx"
},