Reputation: 11884
I use Maven + Spring and I want use RestTemplate().postForEntity(url, request, responseType)
+ Content-Type=application/json
but I have this error:
org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [com.kizeoforms.model.User] and content type [application/json]
java REST client code:
User user = new User();
user.setUser("foo");
user.setPassword("**********");
user.setCompany("xxxxxx");
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add("Content-Type", "application/json");
HttpEntity<User> request = new HttpEntity<User>(user, headers);
ResponseEntity<Object> response = new RestTemplate().postForEntity("https://www.kizeoforms.com:443/rest/v3/login", request, Object.class);
System.out.println(response.getStatusCode());
Upvotes: 1
Views: 2992
Reputation: 1
Look into the Restemplate constructor, if there are supported serialized packaged included in your project, the corresponding message converters will be added. So you can add a dependency package, such as com.google.gson.Gson
or javax.json.bind.Jsonb
, then you needn't handle the message converts explicitly.
Upvotes: 0
Reputation: 11884
I had new MappingJackson2HttpMessageConverter()
to restTemplate
:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<Object> response = restTemplate.postForEntity("https://www.kizeoforms.com:443/rest/v3/login", request, Object.class);
Upvotes: 3