Changes to add fallback option.

This commit is contained in:
Amitabh Tiwari 2022-07-25 07:12:06 +05:30
parent 896ab2d7da
commit 200f802277
6 changed files with 61 additions and 27 deletions

View File

@ -15,20 +15,26 @@ 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);
}
@PostMapping(value = "/upload-error")
public String handleFileUploadError(@RequestPart(value = "file") MultipartFile file) throws NotFoundException {
return service.uploadFileWithCause(file);
@PostMapping(value = "/upload-file")
public boolean handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
return service.uploadFileWithManualClient(file);
}
@PostMapping(value = "/upload-with-fallbackfactory")
public String uploadFileWithFallbackFactory(@RequestPart(value = "file") MultipartFile file)
throws NotFoundException {
return service.uploadFileWithFallbackFactory(file);
}
@PostMapping(value = "/upload-with-fallback")
public String uploadFileWithFallback(@RequestPart(value = "file") MultipartFile file) throws NotFoundException {
return service.uploadFileWithFallback(file);
}
}

View File

@ -8,10 +8,10 @@ import com.baeldung.cloud.openfeign.exception.BadRequestException;
import com.baeldung.cloud.openfeign.exception.NotFoundException;
@Component
public class FileUploadClientFallbackFactory implements FallbackFactory<FileUploadClient> {
public class FileUploadClientFallbackFactory implements FallbackFactory<FileUploadClientWithFallbackFactory> {
@Override
public FileUploadClient create(Throwable cause) {
return new FileUploadClient() {
public FileUploadClientWithFallbackFactory create(Throwable cause) {
return new FileUploadClientWithFallbackFactory() {
@Override
public String fileUpload(MultipartFile file) {
if (cause instanceof BadRequestException) {

View File

@ -8,8 +8,8 @@ import org.springframework.web.multipart.MultipartFile;
import com.baeldung.cloud.openfeign.fileupload.config.FeignSupportConfig;
@FeignClient(name = "file", url = "http://localhost:8081", configuration = FeignSupportConfig.class, fallbackFactory = FileUploadClientFallbackFactory.class)
public interface FileUploadClient {
@FeignClient(name = "file", url = "http://localhost:8080", configuration = FeignSupportConfig.class, fallback = FileUploadClientWithFallbackImpl.class)
public interface FileUploadClientWithFallBack {
@PostMapping(value = "/upload-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String fileUpload(@RequestPart(value = "file") MultipartFile file);
}

View File

@ -8,11 +8,8 @@ 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 {
@FeignClient(name = "file", url = "http://localhost:8080", configuration = FeignSupportConfig.class, fallbackFactory = FileUploadClientFallbackFactory.class)
public interface FileUploadClientWithFallbackFactory {
@PostMapping(value = "/upload-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String fileUpload(@RequestPart(value = "file") MultipartFile file);
@PostMapping(value = "/upload-file-error", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String fileUploadError(@RequestPart(value = "file") MultipartFile file);
}

View File

@ -0,0 +1,31 @@
package com.baeldung.cloud.openfeign.fileupload.service;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import com.baeldung.cloud.openfeign.exception.BadRequestException;
import com.baeldung.cloud.openfeign.exception.NotFoundException;
@Component
public class FileUploadClientWithFallbackImpl implements FileUploadClientWithFallBack {
@Override
public String fileUpload(MultipartFile file) {
try {
throw new NotFoundException("hi, something wrong");
} catch (Exception ex) {
if (ex instanceof BadRequestException) {
return "Bad Request!!!";
}
if (ex instanceof NotFoundException) {
return "Not Found!!!";
}
if (ex instanceof Exception) {
return "Exception!!!";
}
return "Successfully Uploaded file!!!";
}
}
}

View File

@ -12,12 +12,12 @@ import feign.form.spring.SpringFormEncoder;
@Service
public class UploadService {
private static final String HTTP_FILE_UPLOAD_URL = "http://localhost:8081";
private static final String HTTP_FILE_UPLOAD_URL = "http://localhost:8080";
@Autowired
private UploadClient client;
private FileUploadClientWithFallbackFactory fileUploadClient;
@Autowired
private FileUploadClient fileUploadClient;
private FileUploadClientWithFallBack fileUploadClientWithFallback;
public boolean uploadFileWithManualClient(MultipartFile file) {
UploadResource fileUploadResource = Feign.builder().encoder(new SpringFormEncoder())
@ -25,12 +25,12 @@ public class UploadService {
Response response = fileUploadResource.uploadFile(file);
return response.status() == 200;
}
public String uploadFile(MultipartFile file) {
return client.fileUpload(file);
}
public String uploadFileWithCause(MultipartFile file) throws NotFoundException {
public String uploadFileWithFallbackFactory(MultipartFile file) throws NotFoundException {
return fileUploadClient.fileUpload(file);
}
public String uploadFileWithFallback(MultipartFile file) throws NotFoundException {
return fileUploadClientWithFallback.fileUpload(file);
}
}