BAEL-3595: Initial change for Multipart file upload (#10465)

* BAEL-3595: Initial change for Multipart file upload

* Corrected the name

* BAEL-3595: Added Tests

* Corrected the tests name

* Uncommented the code for live test

Co-authored-by: Amitabh Tiwari <Amitabh.Tiwari@maersk.com>
This commit is contained in:
Amitabh Tiwari
2021-02-17 13:09:26 +05:30
committed by GitHub
parent 540ca5df0b
commit 8a2a384dfa
8 changed files with 179 additions and 1 deletions
@@ -0,0 +1,28 @@
package com.baeldung.cloud.openfeign.fileupload.config;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.web.client.RestTemplate;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
@Configuration
public class FeignSupportConfig {
@Bean
@Primary
@Scope("prototype")
public Encoder multipartFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(new ObjectFactory<HttpMessageConverters>() {
@Override
public HttpMessageConverters getObject() {
return new HttpMessageConverters(new RestTemplate().getMessageConverters());
}
}));
}
}
@@ -0,0 +1,28 @@
package com.baeldung.cloud.openfeign.fileupload.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.baeldung.cloud.openfeign.fileupload.service.UploadService;
@RestController
public class FileController {
@Autowired
private UploadService service;
@PostMapping(value = "/upload")
public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
return service.uploadFile(file);
}
@PostMapping(value = "/upload-mannual-client")
public boolean handleFileUploadWithManualClient(
@RequestPart(value = "file") MultipartFile file) {
return service.uploadFileWithManualClient(file);
}
}
@@ -0,0 +1,15 @@
package com.baeldung.cloud.openfeign.fileupload.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import com.baeldung.cloud.openfeign.fileupload.config.FeignSupportConfig;
@FeignClient(name = "file", url = "http://localhost:8081", configuration = FeignSupportConfig.class)
public interface UploadClient {
@PostMapping(value = "/upload-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String fileUpload(@RequestPart(value = "file") MultipartFile file);
}
@@ -0,0 +1,16 @@
package com.baeldung.cloud.openfeign.fileupload.service;
import org.springframework.web.multipart.MultipartFile;
import feign.Headers;
import feign.Param;
import feign.RequestLine;
import feign.Response;
public interface UploadResource {
@RequestLine("POST /upload-file")
@Headers("Content-Type: multipart/form-data")
Response uploadFile(@Param("file") MultipartFile file);
}
@@ -0,0 +1,29 @@
package com.baeldung.cloud.openfeign.fileupload.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import feign.Feign;
import feign.Response;
import feign.form.spring.SpringFormEncoder;
@Service
public class UploadService {
private static final String HTTP_FILE_UPLOAD_URL = "http://localhost:8081";
@Autowired
private UploadClient client;
public boolean uploadFileWithManualClient(MultipartFile file) {
UploadResource fileUploadResource = Feign.builder().encoder(new SpringFormEncoder())
.target(UploadResource.class, HTTP_FILE_UPLOAD_URL);
Response response = fileUploadResource.uploadFile(file);
return response.status() == 200;
}
public String uploadFile(MultipartFile file) {
return client.fileUpload(file);
}
}