Initial Changes for BAEL-5297
This commit is contained in:
parent
6beec696aa
commit
8bd7b32499
|
@ -2,7 +2,9 @@ package com.baeldung.cloud.openfeign.exception;
|
|||
|
||||
public class NotFoundException extends Exception {
|
||||
|
||||
public NotFoundException() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public NotFoundException() {
|
||||
}
|
||||
|
||||
public NotFoundException(String message) {
|
||||
|
|
|
@ -6,6 +6,7 @@ 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.exception.NotFoundException;
|
||||
import com.baeldung.cloud.openfeign.fileupload.service.UploadService;
|
||||
|
||||
@RestController
|
||||
|
@ -26,8 +27,8 @@ public class FileController {
|
|||
}
|
||||
|
||||
@PostMapping(value = "/upload-error")
|
||||
public String handleFileUploadError(@RequestPart(value = "file") MultipartFile file) {
|
||||
return service.uploadFile(file);
|
||||
public String handleFileUploadError(@RequestPart(value = "file") MultipartFile file) throws NotFoundException {
|
||||
return service.uploadFileWithCause(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, fallbackFactory = FileUploadClientFallbackFactory.class)
|
||||
public interface FileUploadClient {
|
||||
@PostMapping(value = "/upload-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
String fileUpload(@RequestPart(value = "file") MultipartFile file);
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.cloud.openfeign.fileupload.service;
|
||||
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
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 FileUploadClientFallbackFactory implements FallbackFactory<FileUploadClient> {
|
||||
@Override
|
||||
public FileUploadClient create(Throwable cause) {
|
||||
return new FileUploadClient() {
|
||||
@Override
|
||||
public String fileUpload(MultipartFile file) {
|
||||
if (cause instanceof BadRequestException) {
|
||||
return "Bad Request!!!";
|
||||
}
|
||||
if (cause instanceof NotFoundException) {
|
||||
return "Not Found!!!";
|
||||
}
|
||||
if (cause instanceof Exception) {
|
||||
return "Exception!!!";
|
||||
}
|
||||
return "Successfully Uploaded file!!!";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -4,6 +4,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.baeldung.cloud.openfeign.exception.NotFoundException;
|
||||
|
||||
import feign.Feign;
|
||||
import feign.Response;
|
||||
import feign.form.spring.SpringFormEncoder;
|
||||
|
@ -14,6 +16,8 @@ public class UploadService {
|
|||
|
||||
@Autowired
|
||||
private UploadClient client;
|
||||
@Autowired
|
||||
private FileUploadClient fileUploadClient;
|
||||
|
||||
public boolean uploadFileWithManualClient(MultipartFile file) {
|
||||
UploadResource fileUploadResource = Feign.builder().encoder(new SpringFormEncoder())
|
||||
|
@ -30,4 +34,7 @@ public class UploadService {
|
|||
return client.fileUpload(file);
|
||||
}
|
||||
|
||||
public String uploadFileWithCause(MultipartFile file) throws NotFoundException {
|
||||
return fileUploadClient.fileUpload(file);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue