今天在使用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. python数字图像处理(15):霍夫线变换

    在图片处理中,霍夫变换主要是用来检测图片中的几何形状,包括直线.圆.椭圆等. 在skimage中,霍夫变换是放在tranform模块内,本篇主要讲解霍夫线变换. 对于平面中的一条直线,在笛卡尔坐标系中 ...

  2. 别出心裁的Linux系统调用学习法

    别出心裁的Linux系统调用学习法 操作系统与系统调用 操作系统(Operating System,简称OS)是计算机中最重要的系统软件,是这样的一组系统程序的集成:这些系统程序在用户对计算机的使用中 ...

  3. java 客户端链接不上redis解决方案

    原文地址:http://blog.csdn.net/yingxiake/article/details/51472810 出现问题描述: 1.Could not get a resource from ...

  4. Log4net使用(一)

    LogHelper.cs using NLog; using NLog.Targets; namespace MyProject.Tool.Log { public class LogHelper { ...

  5. 基本数据类型-列表_元组_字典_day4

    一.列表(list)书写格式:[] #通过list类创建的 li = [1, 12, 9, ", 10, ],"庞麦郎"], "ales", True ...

  6. [转]Null value was assigned to a property of primitive type setter of"原因及解决方法

    原文地址:http://blog.csdn.net/kevinzhangfei/article/details/6995316 在action请求数据的过程中报出"Null value wa ...

  7. [转]SVN版本冲突解决详解

    原文地址:http://blog.csdn.net/windone0109/article/details/4857044 版权声明:本文为博主原创文章,未经博主允许不得转载. 版本冲突原因: 假设A ...

  8. Dubbo系列(2)_RPC介绍

    一.本文目的         主要介绍RPC的一些概念和应用. 二.为什么要介绍RPC    DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治 ...

  9. REST服务返回自定义的HttpResponseMessage

    WebApi框架中对资源的操作,都是通过其Controller提供的各种方法(GET,POST,PUT,DELET等)来实现,而这些方法的返回信息有以下几种形式: 方法返回类型 HttpRespons ...

  10. java判断request请求是手机还是pc终端

    /** * 判断请求是否手机端 * @param req * @return */ public static boolean isMobile(HttpServletRequest req) { U ...