RestTemplate 异常Could not write request: no suitable HttpMessageConverter fo

本文探讨了在使用Spring框架时遇到的HTTP请求抛出异常的问题,特别是当尝试将报文体转换为请求参数时遇到的挑战。通过实例展示了如何正确设置报文体编码类型及使用合适的HttpMessageConverter来解决此问题。

使用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());
	}
}
当出现 `Could not write request: no suitable HttpMessageConverter found for request type [java.io.ByteArrayInputStream]` 错误,通常是因为 Spring 框架在处理请求时,找不到合适的 `HttpMessageConverter` 来将请求类型 `java.io.ByteArrayInputStream` 转换为 HTTP 请求消息。以下是一些可能的解决方法: ### 1. 注册自定义的 `HttpMessageConverter` 可以创建一个自定义的 `HttpMessageConverter` 来处理 `ByteArrayInputStream` 类型的请求。示例代码如下: ```java import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; import org.springframework.http.converter.AbstractHttpMessageConverter; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; import java.io.ByteArrayInputStream; import java.io.IOException; public class ByteArrayInputStreamHttpMessageConverter extends AbstractHttpMessageConverter<ByteArrayInputStream> { public ByteArrayInputStreamHttpMessageConverter() { super(MediaType.APPLICATION_OCTET_STREAM); } @Override protected boolean supports(Class<?> clazz) { return ByteArrayInputStream.class.isAssignableFrom(clazz); } @Override protected ByteArrayInputStream readInternal(Class<? extends ByteArrayInputStream> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { // 这里根据需要实现读取逻辑 return null; } @Override protected void writeInternal(ByteArrayInputStream inputStream, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputMessage.getBody().write(buffer, 0, bytesRead); } } } ``` 然后在配置类中注册这个自定义的 `HttpMessageConverter`: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.util.List; @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new ByteArrayInputStreamHttpMessageConverter()); } } ``` ### 2. 检查请求的 `MediaType` 确保请求的 `MediaType` 与 `HttpMessageConverter` 支持的 `MediaType` 相匹配。可以在请求方法上明确指定 `consumes` 属性,例如: ```java import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import java.io.ByteArrayInputStream; @RestController public class MyController { @PostMapping(value = "/yourEndpoint", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE) public String handleRequest(@RequestBody ByteArrayInputStream inputStream) { // 处理请求 return "Success"; } } ``` ### 3. 检查依赖 确保项目中包含了必要的依赖,特别是 Spring Web 相关的依赖。如果使用 Maven,可以在 `pom.xml` 中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值