Changes to add fallback option.
This commit is contained in:
parent
896ab2d7da
commit
200f802277
|
@ -15,20 +15,26 @@ public class FileController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private UploadService service;
|
private UploadService service;
|
||||||
|
|
||||||
@PostMapping(value = "/upload")
|
|
||||||
public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
|
|
||||||
return service.uploadFile(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
@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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(value = "/upload-error")
|
@PostMapping(value = "/upload-file")
|
||||||
public String handleFileUploadError(@RequestPart(value = "file") MultipartFile file) throws NotFoundException {
|
public boolean handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
|
||||||
return service.uploadFileWithCause(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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -8,10 +8,10 @@ import com.baeldung.cloud.openfeign.exception.BadRequestException;
|
||||||
import com.baeldung.cloud.openfeign.exception.NotFoundException;
|
import com.baeldung.cloud.openfeign.exception.NotFoundException;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class FileUploadClientFallbackFactory implements FallbackFactory<FileUploadClient> {
|
public class FileUploadClientFallbackFactory implements FallbackFactory<FileUploadClientWithFallbackFactory> {
|
||||||
@Override
|
@Override
|
||||||
public FileUploadClient create(Throwable cause) {
|
public FileUploadClientWithFallbackFactory create(Throwable cause) {
|
||||||
return new FileUploadClient() {
|
return new FileUploadClientWithFallbackFactory() {
|
||||||
@Override
|
@Override
|
||||||
public String fileUpload(MultipartFile file) {
|
public String fileUpload(MultipartFile file) {
|
||||||
if (cause instanceof BadRequestException) {
|
if (cause instanceof BadRequestException) {
|
||||||
|
|
|
@ -8,8 +8,8 @@ import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import com.baeldung.cloud.openfeign.fileupload.config.FeignSupportConfig;
|
import com.baeldung.cloud.openfeign.fileupload.config.FeignSupportConfig;
|
||||||
|
|
||||||
@FeignClient(name = "file", url = "http://localhost:8081", configuration = FeignSupportConfig.class, fallbackFactory = FileUploadClientFallbackFactory.class)
|
@FeignClient(name = "file", url = "http://localhost:8080", configuration = FeignSupportConfig.class, fallback = FileUploadClientWithFallbackImpl.class)
|
||||||
public interface FileUploadClient {
|
public interface FileUploadClientWithFallBack {
|
||||||
@PostMapping(value = "/upload-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
@PostMapping(value = "/upload-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||||
String fileUpload(@RequestPart(value = "file") MultipartFile file);
|
String fileUpload(@RequestPart(value = "file") MultipartFile file);
|
||||||
}
|
}
|
|
@ -8,11 +8,8 @@ import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import com.baeldung.cloud.openfeign.fileupload.config.FeignSupportConfig;
|
import com.baeldung.cloud.openfeign.fileupload.config.FeignSupportConfig;
|
||||||
|
|
||||||
@FeignClient(name = "file", url = "http://localhost:8081", configuration = FeignSupportConfig.class)
|
@FeignClient(name = "file", url = "http://localhost:8080", configuration = FeignSupportConfig.class, fallbackFactory = FileUploadClientFallbackFactory.class)
|
||||||
public interface UploadClient {
|
public interface FileUploadClientWithFallbackFactory {
|
||||||
@PostMapping(value = "/upload-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
@PostMapping(value = "/upload-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||||
String fileUpload(@RequestPart(value = "file") MultipartFile file);
|
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);
|
|
||||||
}
|
}
|
|
@ -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!!!";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -12,12 +12,12 @@ import feign.form.spring.SpringFormEncoder;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UploadService {
|
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
|
@Autowired
|
||||||
private UploadClient client;
|
private FileUploadClientWithFallbackFactory fileUploadClient;
|
||||||
@Autowired
|
@Autowired
|
||||||
private FileUploadClient fileUploadClient;
|
private FileUploadClientWithFallBack fileUploadClientWithFallback;
|
||||||
|
|
||||||
public boolean uploadFileWithManualClient(MultipartFile file) {
|
public boolean uploadFileWithManualClient(MultipartFile file) {
|
||||||
UploadResource fileUploadResource = Feign.builder().encoder(new SpringFormEncoder())
|
UploadResource fileUploadResource = Feign.builder().encoder(new SpringFormEncoder())
|
||||||
|
@ -26,11 +26,11 @@ public class UploadService {
|
||||||
return response.status() == 200;
|
return response.status() == 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String uploadFile(MultipartFile file) {
|
public String uploadFileWithFallbackFactory(MultipartFile file) throws NotFoundException {
|
||||||
return client.fileUpload(file);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String uploadFileWithCause(MultipartFile file) throws NotFoundException {
|
|
||||||
return fileUploadClient.fileUpload(file);
|
return fileUploadClient.fileUpload(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String uploadFileWithFallback(MultipartFile file) throws NotFoundException {
|
||||||
|
return fileUploadClientWithFallback.fileUpload(file);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue