我试图发送消息到Ionic Push API,我已经正确构建了Json正文,但它说“没有找到合适的HttpMessageConverter用于请求类型[com。 amazonaws.util.json.JSONObject]”。RestTemplate Java Spring,没有合适的HttpMessageConverter和422 UnprocessableEntity
当我添加一个HttpMessageConverter时,我从Ionic获得一个422 UnprocessableEntity。
下面的代码(在注释中的代码是的MessageConverter):
// MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
// jsonHttpMessageConverter.getObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
// template.getMessageConverters().add(jsonHttpMessageConverter);
headers.add("Authorization", "Bearer <MY API KEY>");
headers.add("Content-Type", "application/json");
String pushMessage = "From : " + notification.getFrom().getNickname() + " Type : " + notification.getNotificationType();
JSONObject body = new JSONObject();
JSONObject message = new JSONObject();
JSONArray deviceTokens = new JSONArray();
try {
deviceTokens.put("DEV-75c960b5-fa90-4da7-b7df-8c1a01f80bdb");
message.put("message", pushMessage);
body.put("notification", message);
body.put("profile", "dev_push");
body.put("tokens", deviceTokens);
} catch (JSONException e) {
e.printStackTrace();
}
template.exchange("https://api.ionic.io/push/notifications", HttpMethod.POST, new HttpEntity<>(body, headers), String.class);
从调试我知道请求的主体由离子
{
"notification": {
"message":"From : mathijs0032 Type : like"
},
"profile": "dev_push",
"tokens": ["DEV-75c960b5-fa90-4da7-b7df-8c1a01f80bdb"]
}
因此,建立结构相匹配问题可能是HttpMessageConverter?