Сообщение RestTemplate для объекта
Мой метод почты вызван, но мой профиль пуст. Что не так с этим подходом? Должен ли я использовать @Requestbody для использования RestTemplate?
Profile profile = new Profile();
profile.setEmail(email);
String response = restTemplate.postForObject("http://localhost:8080/user/", profile, String.class);
@RequestMapping(value = "/", method = RequestMethod.POST)
public @ResponseBody
Object postUser(@Valid Profile profile, BindingResult bindingResult, HttpServletResponse response) {
//Profile is null
return profile;
}
Ответы
Ответ 1
Вы должны создать объект профиля таким образом
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("email", email);
Object response = restTemplate.postForObject("http://localhost:8080/user/", parts, String.class);
Ответ 2
MultiValueMap
была хорошей отправной точкой для меня, но в моем случае она по-прежнему размещала пустой объект в @RestController
мое решение для создания сущности, и публикация выглядела так:
HashedMap requestBody = new HashedMap();
requestBody.put("eventType", "testDeliveryEvent");
requestBody.put("sendType", "SINGLE");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
// Jackson ObjectMapper to convert requestBody to JSON
String json = new ObjectMapper().writeValueAsString(request);
HttpEntity<String> entity = new HttpEntity<>(json, headers);
restTemplate.postForEntity("/generate", entity, String.class);