Stéphane GRILLON
Stéphane GRILLON

Reputation: 11884

java api REST client for POST ERROR: no suitable HttpMessageConverter

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

Answers (2)

Qiang Du
Qiang Du

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

St&#233;phane GRILLON
St&#233;phane GRILLON

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

Related Questions