Java 如何使用RestTemplate对请求主体进行POST调用

Java 如何使用RestTemplate对请求主体进行POST调用,java,httprequest,httpresponse,resttemplate,httpentity,Java,Httprequest,Httpresponse,Resttemplate,Httpentity,我正在尝试使用RestTemplate发出POST请求,以发送请求正文和标题 要做到这一点,我看到许多人正在使用HTTPEntity类。但是这个类是需要传递的泛型类型。内容类型为application/json 在大多数情况下,我看到HttpEntity是使用字符串泛型类型创建的。像这样: HttpEntity<String> requestEntity = new HttpEntity<String>(body, headers); responseEntity = r

我正在尝试使用RestTemplate发出POST请求,以发送请求正文和标题

要做到这一点,我看到许多人正在使用HTTPEntity类。但是这个类是需要传递的泛型类型。内容类型为application/json

在大多数情况下,我看到HttpEntity是使用字符串泛型类型创建的。像这样:

HttpEntity<String> requestEntity = new HttpEntity<String>(body, headers);
responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
在postman中尝试时,该请求有效,但在使用字符串http实体时,在使用RestTemplate的代码中无效

使用JsonObject类型时,出现以下错误:

org.springframework.http.converter.HttpMessageNotWritableException:无法写入JSON:不是JSON原语

当使用字符串类型的http实体时,我收到一些通用错误消息,说请求无效

添加完整的请求正文:

JsonObject body = new JsonObject();
body.addProperty("transaction_id", transaction_id);
body.addProperty("timestamp", timestamp);
body.addProperty("device_token", deviceToken);
if (forUpdate)
    body.addProperty("bit0", true);
邮递员的工作职责:

curl-X柱\ \ -H'内容类型:应用程序/json'\ -H'授权:承载一些\u承载\u令牌\u字符串'\ -H'缓存控制:无缓存'\ -d'{ “设备令牌”:“某些设备令牌”, “交易id”:“f0bc2e5d-5bd9-4437-a455-fd1e210a6268”, “时间戳”:15570737608 }"

有人能帮我理解如何发送这个请求吗

JsonObject body = new JsonObject();
body.addProperty("transaction_id", transaction_id);
body.addProperty("timestamp", timestamp);
body.addProperty("device_token", deviceToken);
if (forUpdate)
    body.addProperty("bit0", true);