JAVA-17818 Merge spring-cloud-modules/open-feign and spring-cloud-modules/open-feign-2 (#13530)

Co-authored-by: timis1 <noreplay@yahoo.com>
This commit is contained in:
timis1
2023-02-26 18:07:17 +02:00
committed by GitHub
parent 45790055ba
commit 62ba63db7f
20 changed files with 2 additions and 151 deletions
@@ -0,0 +1,55 @@
package com.baeldung.cloud.openfeign.fileupload.config;
public class ExceptionMessage {
private String timestamp;
private int status;
private String error;
private String message;
private String path;
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
@Override
public String toString() {
return "ExceptionMessage [timestamp=" + timestamp + ", status=" + status + ", error=" + error + ", message=" + message + ", path=" + path + "]";
}
}
@@ -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.web.client.RestTemplate;
import feign.codec.Encoder;
import feign.codec.ErrorDecoder;
import feign.form.spring.SpringFormEncoder;
public class FeignSupportConfig {
@Bean
public Encoder multipartFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(new ObjectFactory<HttpMessageConverters>() {
@Override
public HttpMessageConverters getObject() {
return new HttpMessageConverters(new RestTemplate().getMessageConverters());
}
}));
}
@Bean
public ErrorDecoder errorDecoder() {
return new RetreiveMessageErrorDecoder();
}
}
@@ -0,0 +1,34 @@
package com.baeldung.cloud.openfeign.fileupload.config;
import java.io.IOException;
import java.io.InputStream;
import com.baeldung.cloud.openfeign.exception.BadRequestException;
import com.baeldung.cloud.openfeign.exception.NotFoundException;
import com.fasterxml.jackson.databind.ObjectMapper;
import feign.Response;
import feign.codec.ErrorDecoder;
public class RetreiveMessageErrorDecoder implements ErrorDecoder {
private final ErrorDecoder errorDecoder = new Default();
@Override
public Exception decode(String methodKey, Response response) {
ExceptionMessage message = null;
try (InputStream bodyIs = response.body()
.asInputStream()) {
ObjectMapper mapper = new ObjectMapper();
message = mapper.readValue(bodyIs, ExceptionMessage.class);
} catch (IOException e) {
return new Exception(e.getMessage());
}
switch (response.status()) {
case 400:
return new BadRequestException(message.getMessage() != null ? message.getMessage() : "Bad Request");
case 404:
return new NotFoundException(message.getMessage() != null ? message.getMessage() : "Not found");
default:
return errorDecoder.decode(methodKey, response);
}
}
}
@@ -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<FileUploadClientWithFallbackFactory> {
@Override
public FileUploadClientWithFallbackFactory create(Throwable cause) {
return new FileUploadClientWithFallbackFactory() {
@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!!!";
}
};
}
}
@@ -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, fallback = FileUploadClientWithFallbackImpl.class)
public interface FileUploadClientWithFallBack {
@PostMapping(value = "/upload-error", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String fileUpload(@RequestPart(value = "file") MultipartFile 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 FileUploadClientWithFallbackFactory {
@PostMapping(value = "/upload-error", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String fileUpload(@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!!!";
}
}
}
@@ -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-error")
@Headers("Content-Type: multipart/form-data")
Response uploadFile(@Param("file") MultipartFile file);
}
@@ -0,0 +1,34 @@
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 FileUploadClientWithFallbackFactory fileUploadClient;
@Autowired
private FileUploadClientWithFallBack fileUploadClientWithFallback;
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 uploadFileWithFallbackFactory(MultipartFile file) {
return fileUploadClient.fileUpload(file);
}
public String uploadFileWithFallback(MultipartFile file) {
return fileUploadClientWithFallback.fileUpload(file);
}
}