【发布时间】:2020-06-25 04:55:36
【问题描述】:
我正在尝试使用 RestTemplate 在 json 对象中发布一个数组
{
"update": {
"name": "xyz",
"id": "C2",
"Description": "aaaaaa",
"members": ["abc", "xyz"]
}
}
这是我的 PostMapping 控制器
@PostMapping(value = "/update")
public Update update(@RequestBody Update update) {
String url = "";
HttpHeaders headers = createHttpHeaders("username", "passowrd");
JSONObject jsonObject = new JSONObject();
jsonObject.put("update", update);
HttpEntity<JSONObject> request = new HttpEntity<>(jsonObject, headers);
ResponseEntity<Update> update = restTemplate.exchange(url, HttpMethod.POST,request, Update.class);
return update.getBody();
}
这是我的 POJO
public class Update {
private String name;
private String id;
private String Descripion;
private List<String> members;
}
我得到 500
{
"timestamp": "2020-03-13T06:31:21.822+0000",
"status": 500,
"error": "Internal Server Error",
"message": "No HttpMessageConverter for org.json.JSONObject and content type \"application/json\""
}
【问题讨论】:
-
能否请您添加构建
RestTemplate实例的代码? -
@Bean public RestTemplate getRestTemp() { return new RestTemplate(); }
-
@POOJA -- 你能把你评论中的信息放到问题中吗?谢谢。
标签: java json spring-boot post resttemplate