How to send Multipart form data and upload PDF with RestTemplate Spring Boot
<p>Good day Pals,
In my microservice and spring-boot app, I have a frontend employee microservice which consumes another microservice with file upload endpoint. 
The calling service is based on spring rest controller and I am trying to consume a File-Upload endpoint using RestTemplate in a Spring Boot application. In a nutshell, trying to upload a PDF file.</p>

<p>I have explored the following SO post, but its not working for me:</p>

<p><a href="https://stackoverflow.com/questions/15261456/jackson-disable-fail-on-empty-beans">jackson disable fail_on_empty_beans</a></p>

<p>I am testing this in postman and getting the following error:</p>

<p>org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer.</p>

<p>Any help will be appreciated pleasee ....</p>

<p>Below are the main components -</p>

Rest Controller ############

<pre><code>        @RestController          
        @RequestMapping(path = “/employee”)          

        public class EmployeeController {
            private EmployeeService empService;

            @RequestMapping(value =“/emp/load”, method = RequestMethod.POST)

public
            @ResponseBody
            ResponseEntity&lt;byte[]&gt; handleFileUpload(         @RequestParam("file") MultipartFile file, @RequestParam String a, @RequestParam String b, @RequestParam String c, @RequestParam String d, @RequestParam String e) throws Exception {         

return empService.handleFileUpload(file, a, b, c, d, e);     

            }
        }
</code></pre>

<p>The service Implementation </p>

<pre><code>     @Service             
     public class EmployeeServiceImpl implements EmployeeService{                      @Value(“${emp.base.url}")                 

    private String EMP_BASE_URI;     

public ResponseEntity&lt;byte[]&gt;handleFileUpload(MultipartFile file, String a, String b, String c, String d, String e) {     

final String uri = EMP_BASE_URI + "/upload";     

            RestTemplate restTemplate = getMappedRestTemplate();     

            MultiValueMap&lt;String, Object&gt; params = new LinkedMultiValueMap&lt;String, Object&gt;();     

            params.add("file", file);     

            params.add(“a”, a);
                 
            params.add(“b”, b);     

            params.add(“c”, c);
                 
            params.add(“d”, d);     

            params.add(“e”, e);

            HttpHeaders headers = new HttpHeaders();     

            headers.set("Content-Type", "multipart/form-data");

            ResponseEntity&lt;byte[]&gt; response = restTemplate.exchange(uri, HttpMethod.POST, new HttpEntity&lt;&gt;(params, headers), byte[].class);

            return new ResponseEntity&lt;&gt;(response.getBody(), response.getStatusCode());
        }                                                             




     private RestTemplate getMappedRestTemplate(){         

                RestTemplate restTemplate = new RestTemplate();         

                ObjectMapper newObjectMapper = new ObjectMapper();

                         newObjectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
                         
                newObjectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);         

                MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter=new MappingJackson2HttpMessageConverter();

                FormHttpMessageConverter formConvertor = new FormHttpMessageConverter();
                         
                restTemplate.getMessageConverters().add(formConvertor);         

                restTemplate.getMessageConverters().add(mappingJacksonHttpMessageConverter);

                restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
                         
                return restTemplate;         
            }
    }
</code></pre>

I am getting the following error:

<p>Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.web.multipart.support.StandardMultipartFile["inputStream"]->java.io.FileInputStream["fd"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.web.multipart.support.StandardMultipartFile["inputStream"]->java.io.FileInputStream["fd"])</p>

<p>Please, any help with be appreciated.
I have been stuck on this all day.</p>