我目前正在与API集成,当我发送创建" order"的POST请求时,我会收到一个JSON响应,如下所示:
{ "OrderCreateResult": 15764514 }
但是,当我将这个JSON映射到POJO时,它看起来像:
@Data
@AllArgsConstructor
public class CreateCircleGraphicsOrderResponse {
private String OrderCreateResult;
}
我通过这样的gson映射它:
if (response.isSuccessful()) {
CreateCircleGraphicsOrderResponse orderResponse = gson.fromJson(response.body().string(), CreateCircleGraphicsOrderResponse.class);
response.close();
return orderResponse;
}
但是,当我通过orderResponse.getOrderCreateResult()检索字符串并打印字符串时,它确实是我期望的字符串,但发送返回的String作为GET请求的查询参数会发送无效请求,如{ {1}}
为什么在映射到POJO时会破坏String,即使它打印的内容与复制和粘贴字符串相同?
这是GET请求:
orderResponse.getOrderCreateResult().equals("15764514") == False
错误信息:
public CircleGraphicsGetOrder getOrder(String circleGraphicsOrderId) {
try {
this.getOrderUrl = this.getOrderUrl.concat(circleGraphicsOrderId);
URL url = new URL(this.getOrderUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
conn.connect();
BufferedReader br = new BufferedReader( new InputStreamReader(
conn.getInputStream(), "UTF-8" ) );
StringBuilder sb = new StringBuilder( 2048 );
for( String line; (line = br.readLine()) != null; ) {
sb.append( line );
}
conn.disconnect();
return null;
} catch(IOException ex) {
throw new IOException(ex.getMessage());
}
}
最终编辑:
我能够通过确保" OrderCreateResult"来解决这个问题。 field是一个int,然后通过调用String.valueOf(orderCreateResult)将它转换为串联阶段的字符串。