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

简介:


今天在使用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.


==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/p/3934649.html,如需转载请自行联系原作者

相关文章
|
6月前
|
Java 应用服务中间件 nginx
【异常解决】java程序连接MinIO报错The request signature we calculated does not match the signature you provided.
【异常解决】java程序连接MinIO报错The request signature we calculated does not match the signature you provided.
560 0
|
7月前
|
网络安全
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://xxxx.svc.cluster.local:8080/xxxx": Connection reset; nested exception is java.net.SocketException: Connection reset 什么原因导致得
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on POST request for "xxxx.svc.cluster.local:8080/xxxx ": Connection reset; nested exception is java.net.SocketException: Connection reset 什么原因导致得
910 0
|
27天前
|
Java 应用服务中间件 Spring
SpringBoot出现 java.lang.IllegalArgumentException: Request header is too large 解决方法
SpringBoot出现 java.lang.IllegalArgumentException: Request header is too large 解决方法
42 0
java.lang.Error: Unresolved compilation problem: The type List is not generic; it cannot be parame
java.lang.Error: Unresolved compilation problem: The type List is not generic; it cannot be parame
|
6月前
|
Java 应用服务中间件
线上临时文件夹报错Failed to parse multipart servlet request; nested exception is java.lang.RuntimeException: java.nio.file.NoSuchFileException
Failed to parse multipart servlet request; nested exception is java.lang.RuntimeException: java.nio.file.NoSuchFileException、tmp、配置文件指定目录
123 0
|
2月前
|
Java 应用服务中间件
Caused by: java.lang.NoClassDefFoundError: org/springframework/web/context/request/async/CallablePro
Caused by: java.lang.NoClassDefFoundError: org/springframework/web/context/request/async/CallablePro 错误处理
62 0
|
6月前
|
Java Docker 微服务
【Java异常】Caused by: java.lang.IllegalArgumentException: method GET must not have a request body
【Java异常】Caused by: java.lang.IllegalArgumentException: method GET must not have a request body
58 1
|
6月前
|
Java 容器
RestTemplate报错I/O error on POST request for "http://crmjob.xxx.xxx.com/removeJob": Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out问题处理
讲述RestTemplate报错I/O error on POST request for "http://crmjob.xxx.xxx.com/removeJob": Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out处理方案
|
7月前
|
XML 开发框架 Java
Java Web 项目入门指南(http、Servlet、Request、Response、ServletContext、会话技术[cookie、session]、Filter、Listener)
Java Web 项目入门指南(http、Servlet、Request、Response、ServletContext、会话技术[cookie、session]、Filter、Listener)
|
3月前
|
存储 前端开发 Java
JavaWeb:Request & Response
在JavaWeb开发中,Request(请求)和Response(响应)是非常重要的概念。它们分别代表着客户端向服务器发送请求和服务器向客户端返回响应的过程。Request对象是由服务器创建的,用于封装来自客户端的请求信息。它包含了请求的HTTP方法(如GET或POST),URL,请求头部、参数等信息。你可以通过Request对象获取客户端发送的表单数据、URL参数、HTTP头部和Cookies等。Response对象则是服务器用来向客户端发送响应的工具。它包含了HTTP状态码、响应头部和响应体等信息。你可以使用Response对象设置响应的状态码、设置响应头部
37 3
 JavaWeb:Request & Response