今天在使用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. 丰富Easyui 的插件 - lookup

    插件用途: 主要用于表单中,某字段的内容是用其他表里的记录ID.当然你可以使用combobox.combotree.combogrid等,但有时这些表现方式并不是很好,希望弹出个层,然后在去做一些查询 ...

  2. osx开发,skport项目记录

    最近在研究前端框架,学习了一下vue.js,而后找到了element.js,后来又了解到了cooking···前端开发真是三天小更新,一周大变样,一个月天翻地覆啊··· 期间在使用cooking时遇到 ...

  3. CSS选择器优先级排列

    CSS选择器的效率从高到低做了一个排序: 1.id选择器(#myid) 2.类选择器(.myclassname) 3.标签选择器(div,h1,p) 4.相邻选择器(h1+p) 5.子选择器(ul & ...

  4. Android studio配置Git

    Android studio配置Git 1.下载window 版git并安装:下载地址 2.Android Studio设置git插件:File->Setting->Version Con ...

  5. CSS截取截取字符长度并显示省略号的方法

    HTML部分 <div> <span>这是一个CSS3截取截取字符的例子.它根据宽度来处理.</span> </div> <div class=& ...

  6. spoj687 后缀数组重复次数最多的连续重复子串

    REPEATS - Repeats no tags  A string s is called an (k,l)-repeat if s is obtained by concatenating k& ...

  7. 510C

    510C 拓扑排序:将那些受影响的字母拓扑排序,其后的输出 #include<iostream> #include<cstdio> #include<vector> ...

  8. lucene-查询query->PhraseQuery多关键字的搜索

    用户在搜索引擎中进行搜索时,常常查找的并非是一个简单的单词,很有可能是几个不同的关键字.这些关键字之间要么是紧密相联,成为一个精确的短 语,要么是可能在这几个关键字之间还插有其他无关的关键字.此时,用 ...

  9. 数据库开发基础-SQl Server 控制数据库的服务+数据库的创建与管理(增删改查)

    控制数据库的服务: 方法一: 1.Windows+R 打开运行  打开cmd 2.输入net start MSSQLserver 启动数据库服务 输入net stop MSSQLserver 关闭数据 ...

  10. Unable to find element on closed window (WARNING: The server did not provide any stacktrace information)

    当你的selenium WebDriver 启动IE11报这个错时:Unable to find element on closed window (WARNING: The server did n ...