用于POST ERROR的java api REST客户端:没有合适的HttpMessageConverter

时间:2017-09-05 16:15:34

标签: java spring rest api

我使用Maven + Spring,我希望使用RestTemplate().postForEntity(url, request, responseType) + Content-Type=application/json 但我有这个错误:

org.springframework.web.client.RestClientException:无法写入请求:找不到合适的HttpMessageConverter请求类型[com.kizeoforms.model.User]和内容类型[application / json] < / p>

java REST客户端代码:

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());

2 个答案:

答案 0 :(得分:1)

我有new MappingJackson2HttpMessageConverter()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);

答案 1 :(得分:0)

查看Restemplate构造函数,如果项目中包含受支持的序列化打包,则将添加相应的消息转换器。因此,您可以添加一个依赖包,例如com.google.gson.Gsonjavax.json.bind.Jsonb,则不必显式处理消息转换。