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.
I have explored the following SO post, but its not working for me:
jackson disable fail_on_empty_beans
I am testing this in postman and getting the following error:
org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer.
Any help will be appreciated pleasee ....
Below are the main components -
Rest Controller ############ @RestController
@RequestMapping(path = “/employee”)
public class EmployeeController {
private EmployeeService empService;
@RequestMapping(value =“/emp/load”, method = RequestMethod.POST)
public
@ResponseBody
ResponseEntity<byte[]> 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);
}
}
The service Implementation
@Service
public class EmployeeServiceImpl implements EmployeeService{
@Value(“${emp.base.url}")
private String EMP_BASE_URI;
public ResponseEntity<byte[]>handleFileUpload(MultipartFile file, String a, String b, String c, String d, String e) {
final String uri = EMP_BASE_URI + "/upload";
RestTemplate restTemplate = getMappedRestTemplate();
MultiValueMap<String, Object> params = new LinkedMultiValueMap<String, Object>();
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<byte[]> response = restTemplate.exchange(uri, HttpMethod.POST, new HttpEntity<>(params, headers), byte[].class);
return new ResponseEntity<>(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;
}
}
I am getting the following error:
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"])
Please, any help with be appreciated. I have been stuck on this all day.
I have gone for sending the params (including the pdf file as a byte stream i.e.
byte[]
) as json in the request body using the following method signature: