Search code examples
javaspring-boothttpclientresttemplate

HttpMessageConverter found for request type [java.lang.Long]


SpringBoot

RestTemplate Converter add FormHttpMessageConverter.java and StringHttpMessageConverter and ResourceHttpMessageConverter and ByteArrayHttpMessageConverter,

Http client Long in params: parts.add("time", time);


org.springframework.http.converter.HttpMessageNotWritableException: Could not write request: no suitable HttpMessageConverter found for request type [java.lang.Long]
    at org.springframework.http.converter.FormHttpMessageConverter.writePart(FormHttpMessageConverter.java:422)
    at org.springframework.http.converter.FormHttpMessageConverter.writeParts(FormHttpMessageConverter.java:393)
    at org.springframework.http.converter.FormHttpMessageConverter.writeMultipart(FormHttpMessageConverter.java:373)
    at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:277)
    at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:95)
    at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:948)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:733)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:414)
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_FORM_URLENCODED));
headers.setAcceptCharset(Collections.singletonList(CharsetUtil.CHARSET_UTF_8));
FileSystemResource zipFile = new FileSystemResource(new File(file.getAbsolutePath()));
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
Long time = System.currentTimeMillis();
parts.add("time", time);
parts.add("md5", SecureUtil.md5(time + "234234").toUpperCase());
parts.add("file", zipFile);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(parts, headers);

What HttpMessageConverter can supports Long ?


Solution

  • By default the following message converters are enabled in spring:

    ByteArrayHttpMessageConverter – converts byte arrays
    StringHttpMessageConverter – converts Strings
    ResourceHttpMessageConverter – converts org.springframework.core.io.Resource for any type of octet stream
    SourceHttpMessageConverter – converts javax.xml.transform.Source
    FormHttpMessageConverter – converts form data to/from a MultiValueMap<String, String>.
    Jaxb2RootElementHttpMessageConverter – converts Java objects to/from XML (added only if JAXB2 is present on the classpath)
    MappingJackson2HttpMessageConverter – converts JSON (added only if Jackson 2 is present on the classpath)
    MappingJacksonHttpMessageConverter – converts JSON (added only if Jackson is present on the classpath)
    AtomFeedHttpMessageConverter – converts Atom feeds (added only if Rome is present on the classpath)
    RssChannelHttpMessageConverter – converts RSS feeds (added only if Rome is present on the classpath)
    

    When receiving a new request, Spring will use the “Accept” header to determine the media type that it needs to respond with.In your case , the FormHttpMessageConverter is the converter selected by default in spring as you have specified the content type.When writing multipart data, this converter uses other HttpMessageConverters to write the respective MIME parts.FormHttpMessageConverter class could not find a message converter for the type Long. So you could try converting the Long into string and assigning it to the part like :

                    Long time = System.currentTimeMillis();
                    parts.add("time", String.valueOf(time));
                    parts.add("md5", SecureUtil.md5(time + "234234").toUpperCase());
                    parts.add("file", zipFile);
                    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(parts, headers);
    

    Doc