>我在json中有以下响应
{
"date": 1590656196885,
"host": "qa1234",
"api": "v8",
"response": {
"type": "PickUp",
"payload": {
"serialNumber": "245547105486858"
}
}
}
这是我的响应类
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.init.models.Response;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class TransactionsResponseDetails extends ErrorResponse {
@JsonProperty("date")
private String date;
@JsonProperty("host")
private String host;
@JsonProperty("api")
private String api;
@JsonProperty("response")
private Response response;
public TransactionsResponseDetails() {}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getApi() {
return api;
}
public void setApi(String api) {
this.api = api;
}
public Response getResponse() {
return response;
}
public void setResponse(Response response) {
this.response = response;
}
}
-
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Response {
@JsonProperty("payload")
private Payload payload;
@JsonProperty("type")
private String type;
public Response() {}
public Payload getPayload() {
return payload;
}
public void setPayload(Payload payload) {
this.payload = payload;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
-
import com.fasterxml.jackson.annotation.JsonProperty;
class Payload {
@JsonProperty("serialNumber")
private String serialNumber;
public Payload() {}
public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}
}
这是与RetroFit的实际通话
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import com.init.TransactionsResponse;
import com.init.models.responses.TransactionsResponseDetails;
.
.
.
Call<TransactionsResponseDetails> Rcall = requestInterface.transactionsDetails(transactionResponseModelForTransactionDetails.get(0));
Rcall.enqueue(new Callback<TransactionsResponseDetails>() {
@Override
public void onResponse(Call<TransactionsResponseDetails> call, Response<TransactionsResponseDetails> response) {
try {
TransactionsResponseDetails transactionsResponseDetails;
if (response.isSuccessful()) {
transactionsResponseDetails = response.body();
} else {
transactionsResponseDetails = ErrorResponse.parseError(response, TransactionsResponseDetails.class);
if (BuildConfig.DEV_STATUS) {
Log.d("TransactionsModelError:", transactionsResponseDetails.getErrorModel().getErrorMessage());
}
}
} catch (Exception ex) {
if (BuildConfig.DEV_STATUS) {
ex.printStackTrace();
}
}
这是我的客户
@Headers({"Content-Type: application/json", "Accept: application/json", BuildConfig.API_BASE_KEY})
@POST("transactions/details")
Call<TransactionsResponseDetails> transactionsDetails(@Body TransactionResponseModelForTransactionDetails transactionResponseModelForTransactionDetails);
这是我的映射器设置
OkHttpClient : private static Retrofit.Builder builder = new Retrofit.Builder() .baseUrl(API_BASE_URL) .addConverterFactory(JacksonConverterFactory.create(getMapper()));
private static ObjectMapper getMapper(){
ObjectMapper mapper = jacksonObjectMapper();
mapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);
return mapper;
}
调用有效。我可以在我的日志中看到 json 响应。 问题是映射仅适用于"日期","主机"和"api"。无论我尝试了什么,"响应"对象都不会被映射。你能帮帮我吗?
请在下面找到我的答案:
依赖
- com.squareup.okhttp3:okhttp:4.6.0
- com.squareup.retrofit2:转换器-杰克逊:2.9.0
- com.squareup.retrofit2:retrofit:2.8.1
改造配置 Java 文件
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new LoggingInterceptor())
.build();
// My Local Test Web Server replace with actual host
final String host = "http://localhost:8080/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(HttpUrl.parse(host))
.client(client)
.addConverterFactory(JacksonConverterFactory.create())
.build();
TransactionService transactionService = retrofit.create(TransactionService.class);
transactionService.getTransactionDetailsResponse()
.enqueue(new Callback<TransactionsResponseDetails>() {
@Override
public void onResponse(Call<TransactionsResponseDetails> call, retrofit2.Response<TransactionsResponseDetails> response) {
System.out.println(response.body().toString());
}
@Override
public void onFailure(Call<TransactionsResponseDetails> call, Throwable throwable) {
throwable.printStackTrace();
}
});
日志记录拦截器
static class LoggingInterceptor implements Interceptor {
private Logger logger = Logger.getLogger(LoggingInterceptor.class.getSimpleName());
@Override
public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
logger.info(String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers()));
okhttp3.Response response = chain.proceed(request);
long t2 = System.nanoTime();
logger.info(String.format("Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1) / 1e6d, response.headers()));
return response;
}
}
接口:
public interface TransactionService {
// My Local path replace with actual path
@GET("hello/data")
Call<TransactionsResponseDetails> getTransactionDetailsResponse();
}
我使用了您的POJO类,因为它们没有发布它们。
我相信您的转换器未在 OkHttpClient 上设置。如果没有,请尝试此答案。
终于我有了答案..! 我认为这是我的杰克逊版本中的一个错误,但我必须在getter和setter中设置@JsonProperty("response"(,因此TransactionsResponseDetails.class应该变成:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.init.models.Response;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class TransactionsResponseDetails extends ErrorResponse {
@JsonProperty("date")
private String date;
@JsonProperty("host")
private String host;
@JsonProperty("api")
private String api;
@JsonProperty("response")
private Response response;
public TransactionsResponseDetails() {}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getApi() {
return api;
}
public void setApi(String api) {
this.api = api;
}
@JsonProperty("response")
public Response getResponse() {
return response;
}
@JsonProperty("response")
public void setResponse(Response response) {
this.response = response;
}
}