Changes to fetch the Original Message

This commit is contained in:
Amitabh Tiwari 2022-02-13 19:04:52 +05:30
parent 7f84edf0f8
commit a6df0b9ff9
6 changed files with 108 additions and 0 deletions

View File

@ -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 + "]";
}
}

View File

@ -7,6 +7,7 @@ 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 {
@ -19,4 +20,9 @@ public class FeignSupportConfig {
}
}));
}
@Bean
public ErrorDecoder errorDecoder() {
return new RetreiveMessageErrorDecoder();
}
}

View File

@ -0,0 +1,35 @@
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);
}
}
}

View File

@ -25,4 +25,9 @@ public class FileController {
return service.uploadFileWithManualClient(file);
}
@PostMapping(value = "/upload-error")
public String handleFileUploadError(@RequestPart(value = "file") MultipartFile file) {
return service.uploadFile(file);
}
}

View File

@ -12,4 +12,7 @@ import com.baeldung.cloud.openfeign.fileupload.config.FeignSupportConfig;
public interface UploadClient {
@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

@ -26,4 +26,8 @@ public class UploadService {
return client.fileUpload(file);
}
public String uploadFileError(MultipartFile file) {
return client.fileUpload(file);
}
}