今天在使用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<String, Object> map = new LinkedMultiValueMap<String, Object>();
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<String, Object> map = new LinkedMultiValueMap<String, Object>();
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.

no suitable HttpMessageConverter found for request type [java.lang.Integer]的更多相关文章

  1. RestTemplate异常no suitable HttpMessageConverter found for request type [java.lang.Integer]

    GET方式,参数必须放在URL后面,http://xxx/list?name={name}&age={age} package com.chelizi.xiruo.xframework.uti ...

  2. org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [41] did not match expected type [java.lang.Integer (n/a)];

    题记:以前记录过一些自己遇到的BUG,这个行为,让我一看报错的提示信息就能定位到问题的所在,后来记得比较多了,好多是重复性的再加上比较忙就没有详细的记录了,今天的工作量比较小,就顺便记录一下,以便以后 ...

  3. org.hibernate.validator.constraints.NotBlank' validating type 'java.lang.Integer

    使用hibernate时,在save方法时,报了:org.hibernate.validator.constraints.NotBlank' validating type 'java.lang.In ...

  4. springMVC 报错:Unknown return value type: java.lang.Integer

    controller层返回值类型为Integer,运行报错: Unknown return value type: java.lang.Integer 解决办法:在此方法上写上注解 @Response ...

  5. springMVC中Unknown return value type: java.lang.Integer(解决)

    controller层返回值类型为Integer,然而报 Unknown return value type: java.lang.Integer 这个错误,500错误 解决办法:在此方法上写上注解@ ...

  6. Validation异常:No validator could be found for constraint '.....' validating type 'java.lang.Integer'.

    javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'java ...

  7. JSON parse error: Cannot deserialize value of type `java.lang.Integer` from Boolean value

    问题原因所在:前端Vue传输的数据字段类型和后端实体类字段不一致. 我的实体类字段是int类型.前端传输的数据是布尔类型. 文章目录 1.后端方法 2.实体类字段 2.前端传输的数据 1.后端方法 @ ...

  8. HV000030: No validator could be found for type: java.lang.Integer.

    在写接口时,一般去查找在类的Integer属性上加了不属于整型的校验,比如@NotEmpty,@Length等 @JSONField(name = "deviceNum") @No ...

  9. ERROR: HHH000091: Expected type: java.sql.Timestamp, actual value: java.lang.Integer

    在将MySql的数据对应到Java的实体类的时候, 遇到了错误 关键点: Hibernate从mysql数据库查出来的Timestamp类型的数据, 会被Hibernate(或者是数据库连接池Drui ...

随机推荐

  1. mac: vmware fusion中cent os启动假死的解决办法

    环境: mac os X 10.9.2 + vmware 6.0.2 + cent OS 6.5 minimal 现象: Booting CentOS (2.6.32-358.e.l6.i686) i ...

  2. 写Java也得了解CPU--CPU缓存

    CPU,一般认为写C/C++的才需要了解,写高级语言的(Java/C#/pathon...)并不需要了解那么底层的东西.我一开始也是这么想的,但直到碰到LMAX的Disruptor,以及马丁的博文,才 ...

  3. win10下 解决系统进程占用80端口

    公司电脑从win7升级到win10,无法启动nginx,日志里输出:2016/05/30 09:26:01 [emerg] 7024#5440: bind() to 0.0.0.0:80 failed ...

  4. PRML读书会第八章 Graphical Models(贝叶斯网络,马尔科夫随机场)

    主讲人 网神 (新浪微博: @豆角茄子麻酱凉面) 网神(66707180) 18:52:10 今天的内容主要是: 1.贝叶斯网络和马尔科夫随机场的概念,联合概率分解,条件独立表示:2.图的概率推断in ...

  5. ASP.NET MVC运行机制源码剖析

    我们都知道ASP.NET首先是从Global.aspx中开始运行的, 在Application_Start()中添加路由映射后, 就由URLRouting组件创建IRouteHandler并执行, 在 ...

  6. .Net分布式异常报警系统-简介

    系统简介 分布式异常报警系统就是收集系统运行过程中产生的未处理异常,检查系统运行的状态,并将异常信息统一发送到服务端,由服务端将信息通知到相关的责任人.  问题 我们在项目开发中可能遇到以下几个问题: ...

  7. PowerShell Script to Deploy Multiple VM on Azure in Parallel #azure #powershell

    Since I need to deploy, start, stop and remove many virtual machines created from a common image I c ...

  8. Verilog代码规范I

    Verilog代码规范I "规范"这问题 "规范"这个富含专业气息的词汇(个人感觉),其实规范这种东西,就是大家都约定熟成的东西,一旦你不遵守这个东西,专业人士 ...

  9. [BZOJ 2819]NIM(dfs序维护树上xor值)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2819 分析: 树上的nim游戏,关键就是要判断树上的一条链的异或值是否为0 这个题目有 ...

  10. [BZOJ1232][[Usaco2008Nov]安慰奶牛cheer(MST)

    题目:http://hzwer.com/2493.html 分析:对于每条边,贡献的价值是这条边的边权加上这条边连接的两点的权值,所以可以把每条边的边权加上两顶点的点权作为新的边权,然后跑个最小生成树 ...