博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
no suitable HttpMessageConverter found for request type [java.lang.Integer]
阅读量:6070 次
发布时间:2019-06-20

本文共 3608 字,大约阅读时间需要 12 分钟。

今天在使用Spring Template的时候遇到了这个异常:

no suitable HttpMessageConverter found for request type [java.lang.Integer]

Google了一下然后在stackoverflow上面找到了解决方案:

I have a method in Spring rest service.

@RequestMapping(value = "test/process", method = RequestMethod.POST)  public @ResponseBody MyResponse processRequest(String RequestId, int count)

I am using Spring RestTemplate to call this service like this.

RestTemplate restTemplate = this.getRestTemplate();  MultiValueMap
map = new LinkedMultiValueMap
(); map.add("RequestId", RequestId); map.add("count", count); restTemplate.postForObject(url, map,MyResponse.class);

When I try to invoke the client method I get the exception that no suitable HttpMessageConverter found for request type [java.lang.Integer]

org.springframework.http.converter.HttpMessageNotWritableException: Could not write request: no suitable HttpMessageConverter found for request type [java.lang.Integer]at org.springframework.http.converter.FormHttpMessageConverter.writePart(FormHttpMessageConverter.java:310)at org.springframework.http.converter.FormHttpMessageConverter.writeParts(FormHttpMessageConverter.java:270)at org.springframework.http.converter.FormHttpMessageConverter.writeMultipart(FormHttpMessageConverter.java:260)at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:200)at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:1)at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:596)

I know one of the ways is to pass all the parameters as String. But I might need to pass complex data types as parameters later. What is the ways to achieve this. I have googled and some option seem to be writing my own converters. How should I start about solving this problem.

解决办法

The root cause of this error is that by specifying an Integer in the LinkedMultiValueMap, the RestTemplate will take that to mean that your request is a multipart request. There is no HttpMessageConverter registered by default that can handle writing values of type Integer to a request body.

As you said, you can handle this situation by changing the count to be a String. After all, there is no Integer type in HTTP request parameters. However, you were worried

But I might need to pass complex data types as parameters later.

Assume something like this

public @ResponseBody MyResponse processRequest(String RequestId, int count, Complex complex)

with

public class Complex {    private String someValue;    private int intValue;    public String getSomeValue() {        return someValue;    }    public void setSomeValue(String someValue) {        this.someValue = someValue;    }    public int getIntValue() {        return intValue;    }    public void setIntValue(int intValue) {        this.intValue = intValue;    }    public String toString() {        return someValue + " " + intValue;    }}

The the following will work just fine

MultiValueMap
map = new LinkedMultiValueMap
();map.add("RequestId", "asd");map.add("count", "42");map.add("someValue", "complex");map.add("intValue", "69");restTemplate.postForObject(url, map,MyResponse.class);

Remember that the request parameters are used to populate the fields of model attributes by their names.

An even better solution would have you using a serialization standard like JSON or XML.I mean instead of sending request parameters, you should send JSON and XML. Spring supports those types with @RequestBody annotation on handler method parameters.

转载地址:http://bvygx.baihongyu.com/

你可能感兴趣的文章
好用免费的思维导图工具,多人协作共同编辑
查看>>
自己生成非官方iPhone toolchain的头文件
查看>>
安装性能测试工具:sysbench和使用apache的ab
查看>>
[转载]Struts2的配置
查看>>
C# 连接SQL数据库
查看>>
Oracle数据库的权限、用户、角色管理
查看>>
对文章内容进行分页
查看>>
数组方式使用jQuery对象
查看>>
layui-更多demo
查看>>
电脑 F键(功能键)的具体作用
查看>>
STM32F207Cube库函数USART中断接收
查看>>
比较两个圆的面积大小
查看>>
ble设备的第一次成功例子
查看>>
JVM自动内存管理机制
查看>>
loj2542「PKUWC2018」随机游走
查看>>
bzoj2564集合的面积
查看>>
Vue搭建后台项目
查看>>
app启动过程
查看>>
docker的四种网络模式
查看>>
MVC,MVP 和 MVVM 的图示
查看>>