Changes for fallback option

This commit is contained in:
Amitabh Tiwari 2022-07-25 07:15:20 +05:30
parent 200f802277
commit 07b65a6d33
3 changed files with 10 additions and 16 deletions

View File

@ -6,7 +6,6 @@ import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import com.baeldung.cloud.openfeign.exception.NotFoundException;
import com.baeldung.cloud.openfeign.fileupload.service.UploadService; import com.baeldung.cloud.openfeign.fileupload.service.UploadService;
@RestController @RestController
@ -16,8 +15,7 @@ public class FileController {
private UploadService service; private UploadService service;
@PostMapping(value = "/upload-mannual-client") @PostMapping(value = "/upload-mannual-client")
public boolean handleFileUploadWithManualClient( public boolean handleFileUploadWithManualClient(@RequestPart(value = "file") MultipartFile file) {
@RequestPart(value = "file") MultipartFile file) {
return service.uploadFileWithManualClient(file); return service.uploadFileWithManualClient(file);
} }
@ -27,13 +25,12 @@ public class FileController {
} }
@PostMapping(value = "/upload-with-fallbackfactory") @PostMapping(value = "/upload-with-fallbackfactory")
public String uploadFileWithFallbackFactory(@RequestPart(value = "file") MultipartFile file) public String uploadFileWithFallbackFactory(@RequestPart(value = "file") MultipartFile file) {
throws NotFoundException {
return service.uploadFileWithFallbackFactory(file); return service.uploadFileWithFallbackFactory(file);
} }
@PostMapping(value = "/upload-with-fallback") @PostMapping(value = "/upload-with-fallback")
public String uploadFileWithFallback(@RequestPart(value = "file") MultipartFile file) throws NotFoundException { public String uploadFileWithFallback(@RequestPart(value = "file") MultipartFile file) {
return service.uploadFileWithFallback(file); return service.uploadFileWithFallback(file);
} }

View File

@ -4,8 +4,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import com.baeldung.cloud.openfeign.exception.NotFoundException;
import feign.Feign; import feign.Feign;
import feign.Response; import feign.Response;
import feign.form.spring.SpringFormEncoder; import feign.form.spring.SpringFormEncoder;
@ -26,11 +24,11 @@ public class UploadService {
return response.status() == 200; return response.status() == 200;
} }
public String uploadFileWithFallbackFactory(MultipartFile file) throws NotFoundException { public String uploadFileWithFallbackFactory(MultipartFile file) {
return fileUploadClient.fileUpload(file); return fileUploadClient.fileUpload(file);
} }
public String uploadFileWithFallback(MultipartFile file) throws NotFoundException { public String uploadFileWithFallback(MultipartFile file) {
return fileUploadClientWithFallback.fileUpload(file); return fileUploadClientWithFallback.fileUpload(file);
} }
} }

View File

@ -33,7 +33,7 @@ public class OpenFeignFileUploadLiveTest {
FileInputStream input = new FileInputStream(file); FileInputStream input = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
IOUtils.toByteArray(input)); IOUtils.toByteArray(input));
Assert.assertTrue(uploadService.uploadFileWithManualClient(multipartFile)); uploadService.uploadFileWithFallback(multipartFile);
} }
@Test @Test
@ -44,7 +44,6 @@ public class OpenFeignFileUploadLiveTest {
FileInputStream input = new FileInputStream(file); FileInputStream input = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
IOUtils.toByteArray(input)); IOUtils.toByteArray(input));
String uploadFile = uploadService.uploadFile(multipartFile); uploadService.uploadFileWithFallbackFactory(multipartFile);
Assert.assertNotNull(uploadFile);
} }
} }