使用RestTemplate发送Http请求抛出异常:Exception in thread "main" org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type
原因:根据Http请求ContentType找到的HttpMessageConverter无法处理报文体数据导致。HttpMessageConverter作用是根据ContentType类型,只负责将报文体内容转换到请求参数中,转换之后就可以通过@RequestParam或request.getParameter获取,否则只能通过@RequestBody注解获取。
Http请求包括的内容如下:
报文体经常用到的情形是发送Post请求时,Post请求参数将作为报文体传递,所以Post请求参数的长度才没有限制。
HttpHeaders默认的ContentType为application/x-www-form-urlencoded,其中application/x-www-form-urlencoded是一种编码类型,当URL地址里包含非西欧字符的字符串时,系统会将这些字符转换成application/x-www-form-urlencoded字符串。表单里提交时也是如此,当包含非西欧字符的字符串时,系统也会将这些字符转换成application/x-www-form-urlencoded字符串。然而,在向服务器发送大量的文本、包含非ASCII字符的文本或二进制数据时这种编码方式效率很低。这个时候我们就要使用另一种编码类型“multipart/form-data”,比如在我们在做上传的时候,表单的enctype属性一般会设置成“multipart/form-data”。Browser端<form>表单的ENCTYPE属性值为multipart/form-data,它告诉我们传输的数据要用到多媒体传输协议,由于多媒体传输的都是大量的数据,所以规定上传文件必须是post方法。
为了能够将报文体转换到请求参数中,并且通过request.getParameter方法获取报文体中内容,将报文体内容设置为String,如果是实体类转换为String再传递。实例如下:
传递的实体类:
package com; public class IndexInfo { private String name ; private String content ; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Override public String toString() { return "name="+ name+"&content="+content; } }
Controller类和Main方法:
package com; import java.nio.charset.Charset; import java.util.Collections; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.client.RestTemplate; @Controller @RequestMapping("/test") public class TestController { @RequestMapping @ResponseBody public IndexInfo testMethod(IndexInfo indexInfo,@RequestBody String requestBody){ System.out.println("indexinfo name -> "+indexInfo.getName()+" ;content -> "+indexInfo.getContent()); System.out.println("request body -> "+requestBody); return indexInfo ; } public static void main(String[] args) { RestTemplate restTemplate = new RestTemplate() ; //请求的url String url = "http://localhost:8080/gisserver_jpa/test" ; //设置Http请求头和报文体 HttpHeaders httpHeaders = new HttpHeaders() ; //设置HTTP请求的请求头信息 httpHeaders.setContentType(MediaType.parseMediaType("application/x-www-form-urlencoded;charset=UTF-8")); //设置相应内容,相应内容将被转换为json格式返回 httpHeaders.setAcceptCharset(Collections.singletonList(Charset.forName("UTF-8"))); httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); //创建要传递的对象 IndexInfo indexInfo = new IndexInfo(); indexInfo.setName("0123456789"); indexInfo.setContent("new content"); //设置HttpEntity的Body类型为String,调用StringHttpMessageConverter转换报文体参数 HttpEntity<String> httpEntity = new HttpEntity(indexInfo.toString(),httpHeaders) ; //发送post请求,并将返回的实体类型设置的IndexInfo indexInfo = restTemplate.postForObject(url, httpEntity, IndexInfo.class) ; System.out.println(indexInfo.toString()); } }