Spring 接受Hashmap作为POST调用的主体
我必须写一个程序,它将接受Hashmap格式的body。 我已经用Hashmap值和头创建了HttpEntitySpring 接受Hashmap作为POST调用的主体,spring,resttemplate,Spring,Resttemplate,我必须写一个程序,它将接受Hashmap格式的body。 我已经用Hashmap值和头创建了HttpEntity public <T> void doPOSTRequest(String url, T body, HttpHeaders headers) throws JsonProcessingException { HttpEntity<T> request = new HttpEntity<T>(body,headers); Syste
public <T> void doPOSTRequest(String url, T body, HttpHeaders headers) throws JsonProcessingException {
HttpEntity<T> request = new HttpEntity<T>(body,headers);
System.out.println("Printing Request :" + request);
ResponseEntity<String> response = null;
//Calling POST Method
//response=restTemplate.postForObject(url,request,String.class);
response=restTemplate.exchange(url, HttpMethod.POST,request,String.class);
System.out.println(response);
}
public void doPOSTRequest(字符串url、T body、HttpHeaders)抛出JsonProcessingException{
HttpEntity请求=新的HttpEntity(主体、标题);
System.out.println(“打印请求:+请求”);
ResponseEntity response=null;
//调用POST方法
//response=restTemplate.postForObject(url、请求、字符串、类);
response=restemplate.exchange(url,HttpMethod.POST,request,String.class);
System.out.println(响应);
}
我面临以下例外:
线程“main”中出现异常
org.springframework.web.client.RestClientException:否
位于[java.util.HashMap]的HttpMessageConverter
org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:957)
在
org.springframework.web.client.restemplate.doExecute(restemplate.java:733)
在
org.springframework.web.client.restemplate.execute(restemplate.java:670)
在
org.springframework.web.client.restemplate.exchange(restemplate.java:579)
位于Com.RESTRequest.doPOSTRequest(RESTRequest.java:39)
main(GenericREST.java:30)
尝试使用
MultiValueMap
而不是像这样的泛型
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<MultiValueMap<String, Object>>(parameters, headers);
HttpEntity entity=新的HttpEntity(参数、标题);
完整块:
public <T> void doPOSTRequest(String url, T body, HttpHeaders headers) throws JsonProcessingException {
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(body,headers);
System.out.println("Printing Request :" + request);
ResponseEntity<String> response = null;
//Calling POST Method
//response=restTemplate.postForObject(url,request,String.class);
response=restTemplate.exchange(url, HttpMethod.POST,request,String.class);
System.out.println(response);
}
public void doPOSTRequest(字符串url、T body、HttpHeaders)抛出JsonProcessingException{
HttpEntity请求=新的HttpEntity(主体、标题);
System.out.println(“打印请求:+请求”);
ResponseEntity response=null;
//调用POST方法
//response=restTemplate.postForObject(url、请求、字符串、类);
response=restemplate.exchange(url,HttpMethod.POST,request,String.class);
System.out.println(响应);
}
尝试使用多值映射
而不是像这样的泛型
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<MultiValueMap<String, Object>>(parameters, headers);
HttpEntity entity=新的HttpEntity(参数、标题);
完整块:
public <T> void doPOSTRequest(String url, T body, HttpHeaders headers) throws JsonProcessingException {
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(body,headers);
System.out.println("Printing Request :" + request);
ResponseEntity<String> response = null;
//Calling POST Method
//response=restTemplate.postForObject(url,request,String.class);
response=restTemplate.exchange(url, HttpMethod.POST,request,String.class);
System.out.println(response);
}
public void doPOSTRequest(字符串url、T body、HttpHeaders)抛出JsonProcessingException{
HttpEntity请求=新的HttpEntity(主体、标题);
System.out.println(“打印请求:+请求”);
ResponseEntity response=null;
//调用POST方法
//response=restTemplate.postForObject(url、请求、字符串、类);
response=restemplate.exchange(url,HttpMethod.POST,request,String.class);
System.out.println(响应);
}
感谢您的回复。但我把Map作为输入参数。由于依赖关系,我无法更改。在这种情况下,请查看为POST请求头传递的内容类型,并感谢响应。但我把Map作为输入参数。由于依赖关系,我无法更改。在这种情况下,请查看POST请求头W传递的内容类型