RestTemplate报错:no suitable HttpMessageConverter found for request type

本文通过一次RestTemplate调试经历,详细介绍了如何解决使用MultiValueMap作为请求参数时出现的异常问题,并提供了一种可行的解决方案。

某天试用RestTemplate调试,浏览器直接提示:

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Dec 01 14:49:26 CST 2017

There was an unexpected error (type=Internal Server Error, status=500).

Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.util.LinkedMultiValueMap] and content type [application/x-www-form-urlencoded;charset=UTF-8]

 

查看后台是RestTemplate模板异常:

org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.util.LinkedMultiValueMap] and content type [application/x-www-form-urlencoded;charset=UTF-8]

 

异常提示已经很明显了,原因是由于我的post方法使用了MultiValueMap<String, Object>来封装参数,但是无法找到合适的类型转换,仔细查看我的RestTemplate对象获取方式是如下的:

     public static RestTemplate getInstance(String charset) {
          StringHttpMessageConverter m = new StringHttpMessageConverter(Charset.forName(charset));
          RestTemplate restTemplate = new RestTemplateBuilder().additionalMessageConverters(m).build();
          return restTemplate;
     }

 

 

其实问题就出现在上面这段代码上面,我使用了RestTemplateBuilder来创建RestTemplate对象,该方式只会为restTemplate模板初始化一个HttpMessageConverter(注:StringHttpMessageConverter间接实现HttpMessageConverter接口)

 

 

再比较直接 new RestTemplate()创建对象的方式

 

找到原因所在,可以不使用RestTemplateBuilder创建对象来避免这个异常,为此对getInstance方法改造成以下的形式:

    public static RestTemplate getInstanceByCharset(String charset) {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new StringHttpMessageConverter(Charset.forName(charset)));
        return restTemplate;
    }

后台不再报错,问题暂时解决,但不完美。这种写法无法解决中文乱码问题,且看另一篇文章 初探RestTemplate--解决中文乱码问题 ,文章末尾实现了一种可靠的构建RestTemplate方案。

 

当出现 `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
发出的红包

打赏作者

Alphathur

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值