Мне нужна ваша помощь. У меня есть 2 Api в том же классе, оба используют для загрузки файла. Я пытаюсь загрузить файл из 1 API в другие API с помощью RestTemplate
, но это ошибка показа, как.
Ошибка: - MessageType definition error: [simple type, class java.io.FileDescriptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: 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.StandardMultipartHttpServletRequest$StandardMultipartFile["inputStream"]->java.io.FileInputStream["fd"])
Файловый магазин Api не звонит через RestTemplate
из-за вышеуказанной ошибки.
API-1: - Файловый магазин Api
@RequestMapping(value = PathConst.UPLOAD_MULTIPART_FILE, method = RequestMethod.POST)
public ResponseEntity<?> uploadMultipleFiles(@RequestPart("files") @NotNull List<MultipartFile> files) {
try {
this.apiResponse = new APIResponse();
// Note :- 'here this upload file dto help to collect the non-support file info'
List<UploadFileDto> wrongTypeFile = files.stream().
filter(file -> {
return !isSupportedContentType(file.getContentType());
}).map(file -> {
UploadFileDto wrongTypeFileDto = new UploadFileDto();
wrongTypeFileDto.setSize(String.valueOf(file.getSize()));
wrongTypeFileDto.setFileName(file.getOriginalFilename());
wrongTypeFileDto.setContentType(file.getContentType());
return wrongTypeFileDto;
}).collect(Collectors.toList());
if(!wrongTypeFile.isEmpty()) { throw new FileStorageException(wrongTypeFile.toString()); }
this.apiResponse.setReturnCode(HttpStatus.OK.getReasonPhrase());
this.apiResponse.setMessage("File Store Success full");
this.apiResponse.setEntity(
files.stream().map(file -> {
String fileName = this.fileStoreManager.storeFile(file);
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath().path(PathConst.FILE + PathConst.DOWNLOAD_FILE).path(fileName).toUriString();
UploadFileDto uploadFile = new UploadFileDto();
uploadFile.setFileName(fileName);
uploadFile.setUrl(fileDownloadUri);
uploadFile.setSize(String.valueOf(file.getSize()));
return uploadFile;
}).collect(Collectors.toList()));
}catch (Exception e) {
System.out.println("Message" + e.getMessage());
this.errorResponse = new ExceptionResponse();
this.errorResponse.setErrorCode(HttpStatus.BAD_REQUEST.getReasonPhrase());
this.errorResponse.setErrorMessage("Sorry! Filename contains invalid Type's,");
this.errorResponse.setErrors(Collections.singletonList(e.getMessage()));
return new ResponseEntity<ExceptionResponse>(this.errorResponse, HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<APIResponse>(this.apiResponse, HttpStatus.OK);
}
API-2: - API-интерфейс магазина продуктов со списком файлов изображений
@RequestMapping(value = "/save/product", method = RequestMethod.POST)
public ResponseEntity<?> saveProduct(@Valid @RequestPart("request") ProductDto productDto, @RequestPart("files") @NotNull List<MultipartFile> files) {
try {
this.apiResponse = new APIResponse();
this.restTemplate = new RestTemplate();
if(!files.isEmpty()) { new NullPointerException("List of File Empty"); }
// call-api of File controller
try {
// file's
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
files.stream().forEach(file -> { body.add("files", file); });
// header-type
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
// request real form
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(body, headers);
// request-url
this.RESOURCE_URL = ServletUriComponentsBuilder.fromCurrentContextPath().path(PathConst.FILE + PathConst.UPLOAD_MULTIPART_FILE).toUriString();
// response-result
System.out.println(this.restTemplate.exchange(this.RESOURCE_URL, HttpMethod.POST, request, Object.class).getStatusCode());
// if(!response.getStatusCode().equals(200)) {
// // error will send
// }
}catch (NullPointerException e) {
throw new NullPointerException("Product Image's Not Save Scuessfully. " + e);
}
}catch (Exception e) {
System.out.println("Message" + e.getMessage());
this.errorResponse = new ExceptionResponse();
this.errorResponse.setErrorCode(HttpStatus.NO_CONTENT.getReasonPhrase());
this.errorResponse.setErrorMessage(e.getMessage());
return new ResponseEntity<ExceptionResponse>(this.errorResponse, HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<APIResponse>(this.apiResponse, HttpStatus.OK);
}
Основной код: -
try {
// file's
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
files.stream().forEach(file -> { body.add("files", file); });
// header-type
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
// request real form
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(body, headers);
// request-url
this.RESOURCE_URL = ServletUriComponentsBuilder.fromCurrentContextPath().path(PathConst.FILE + PathConst.UPLOAD_MULTIPART_FILE).toUriString();
// response-result
System.out.println(this.restTemplate.exchange(this.RESOURCE_URL, HttpMethod.POST, request, Object.class).getStatusCode());
//if(!response.getStatusCode().equals(200)) {
// // error will send
//}
}catch (NullPointerException e) {
throw new NullPointerException("Product Image's Not Save Scuessfully. " + e);
}