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)
AI 代码解读

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);
AI 代码解读

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)
AI 代码解读

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) 
AI 代码解读

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;
    }
}
AI 代码解读

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);
AI 代码解读

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.

目录
打赏
0
0
0
0
20
分享
相关文章
java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must b
java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must b
282 0
日常报错 - NO constructor found in matching [java.lang.String]解决方法
日常报错 - NO constructor found in matching [java.lang.String]解决方法
605 0
|
10月前
java.lang.IllegalArgumentException: Invalid character found in method name
java.lang.IllegalArgumentException: Invalid character found in method name
215 0
Failed to instantiate [java.lang.Long]: No default constructor found;nested exception is java.lang()
Failed to instantiate [java.lang.Long]: No default constructor found;nested exception is java.lang()
Failed to instantiate [java.lang.Long]: No default constructor found;nested exception is java.lang()
控制台报错 No constructor found in com.base.entity.Menu matching [java.lang.Integer, java.lang.String]
控制台报错 No constructor found in com.base.entity.Menu matching [java.lang.Integer, java.lang.String]
182 0
控制台报错 No constructor found in com.base.entity.Menu matching [java.lang.Integer, java.lang.String]
types. Found: ‘com.baomidou.mybatisplus.annotation.IdType‘, required: ‘java.lang.String‘
被自己蠢哭了,今天设置表的主键为自动递增,就写IdType,死活报 types. Found: 'com.baomidou.mybatisplus.annotation.IdType', required: 'java.lang.String' 这个错误。
169 0
IllegalArgumentException Parameter value [4] did not match expected type [java.lang.String (na)]
IllegalArgumentException Parameter value [4] did not match expected type [java.lang.String (na)]
java.lang.IllegalArgumentException: Unsupported class file major version 60
java.lang.IllegalArgumentException: Unsupported class file major version 60
1087 0
java.lang.IllegalArgumentException: Unsupported class file major version 60
Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate';
Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate';
812 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等