Я хотел бы загрузить файл, но я все еще получаю «Не удалось преобразовать запрос: не найден подходящий HttpMessageConverter для ожидаемого типа [org.springframework.web.multipart.MultipartFile]» исключение
Как можно решить эту проблему?
private RequestMapping createMappingFile(HttpMethod[] method, String... path) {
RequestMapping requestMapping = new RequestMapping();
requestMapping.setMethods(method);
requestMapping.setConsumes(MediaType.MULTIPART_FORM_DATA_VALUE);
requestMapping.setProduces(MediaType.APPLICATION_JSON_VALUE);
requestMapping.setPathPatterns(path);
return requestMapping;
}
@Bean
public IntegrationFlow httpPostFileUpload() {
return IntegrationFlows.from(httpPostGateFileUpload())
.channel("http.file.upload.channel").handle("fileEndpoint", "upload").get();
}
@Bean
public MessagingGatewaySupport httpPostGateFileUpload() {
// @RequestMapping
HttpRequestHandlingMessagingGateway handler = new HttpRequestHandlingMessagingGateway();
handler.setRequestMapping(createMappingFile(new HttpMethod[]{HttpMethod.POST}, "/api/file/upload"));
handler.setStatusCodeExpression(fileParser().parseExpression("T(org.springframework.http.HttpStatus).BAD_REQUEST"));
handler.setRequestPayloadType(ResolvableType.forClass(MultipartFile.class));
handler.setHeaderMapper(fileHeaderMapper());
return handler;
}
FileEndpoint:
public Message<?> upload(Message<MultipartFile>msg) {
MultipartFile file = msg.getPayload();
UserDetails loggedUser = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
Optional<User> opUser = userRepository.findByUsername(loggedUser.getUsername());
User user = opUser.orElseThrow(()->new ResponseStatusException(HttpStatus.NOT_FOUND,"User not found"));
long date = new Date().getTime();
File convertFile = new File(resourcePath+date+"_"+file.getOriginalFilename());
try {
FileOutputStream fout = new FileOutputStream(convertFile);
fout.write(file.getBytes());
fout.close();
...
Я запихну файл в каталог.
Исключение:
org.springframework.messaging.MessagingException: Could not convert request: no suitable HttpMessageConverter found for expected type [org.springframework.web.multipart.MultipartFile] and content type [multipart/form-data;boundary=--------------------------525212308988156732836650;charset=UTF-8]
at org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport.extractRequestBody(HttpRequestHandlingEndpointSupport.java:513) ~[spring-integration-http-5.1.6.RELEASE.jar:5.1.6.RELEASE]
```