[英]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: 我使用Maven + Spring,并且我想使用
RestTemplate().postForEntity(url, request, responseType)
+ Content-Type=application/json
但是我RestTemplate().postForEntity(url, request, responseType)
此错误:
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] org.springframework.web.client.RestClientException:无法编写请求:找不到适合请求类型[com.kizeoforms.model.User]和内容类型[application / json]的HttpMessageConverter
java REST client code: 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());
I had new MappingJackson2HttpMessageConverter()
to restTemplate
: 我有
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);
Look into the Restemplate constructor, if there are supported serialized packaged included in your project, the corresponding message converters will be added. 查看Restemplate构造函数,如果项目中包含受支持的序列化打包,则将添加相应的消息转换器。 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. 因此,您可以添加一个依赖包,例如
com.google.gson.Gson
或javax.json.bind.Jsonb
,则无需显式处理消息转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.