Merge pull request #12474 from amitiw4u/BAEL-5297_ExceptionHandling
Bael 5297 exception handling
This commit is contained in:
commit
e17700100f
@ -52,6 +52,7 @@
|
||||
<module>spring-cloud-bus</module>
|
||||
<module>spring-cloud-data-flow</module>
|
||||
<module>spring-cloud-sleuth</module>
|
||||
<module>spring-cloud-openfeign-2</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
|
1
spring-cloud-modules/spring-cloud-openfeign-2/README.md
Normal file
1
spring-cloud-modules/spring-cloud-openfeign-2/README.md
Normal file
@ -0,0 +1 @@
|
||||
### Relevant Articles:
|
64
spring-cloud-modules/spring-cloud-openfeign-2/pom.xml
Normal file
64
spring-cloud-modules/spring-cloud-openfeign-2/pom.xml
Normal file
@ -0,0 +1,64 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.cloud</groupId>
|
||||
<artifactId>spring-cloud-openfeign-2</artifactId>
|
||||
<name>spring-cloud-openfeign-2</name>
|
||||
<description>OpenFeign project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-okhttp</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign.form</groupId>
|
||||
<artifactId>feign-form</artifactId>
|
||||
<version>${feign-form.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign.form</groupId>
|
||||
<artifactId>feign-form-spring</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring-cloud.version>2021.0.0</spring-cloud.version>
|
||||
<feign-form.version>3.8.0</feign-form.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,16 @@
|
||||
package com.baeldung.cloud.openfeign;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
public class ExampleApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ExampleApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.cloud.openfeign.exception;
|
||||
|
||||
public class BadRequestException extends Exception {
|
||||
|
||||
public BadRequestException() {
|
||||
}
|
||||
|
||||
public BadRequestException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public BadRequestException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BadRequestException: " + getMessage();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.baeldung.cloud.openfeign.exception;
|
||||
|
||||
public class NotFoundException extends Exception {
|
||||
|
||||
public NotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public NotFoundException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NotFoundException: " + getMessage();
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
server.port=8085
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
spring.application.name= openfeign
|
||||
logging.level.com.baeldung.cloud.openfeign.client: DEBUG
|
||||
feign.hystrix.enabled=true
|
||||
|
||||
spring.security.oauth2.client.registration.keycloak.authorization-grant-type=client_credentials
|
||||
spring.security.oauth2.client.registration.keycloak.client-id=payment-app
|
||||
spring.security.oauth2.client.registration.keycloak.client-secret=863e9de4-33d4-4471-b35e-f8d2434385bb
|
||||
spring.security.oauth2.client.provider.keycloak.token-uri=http://localhost:8083/auth/realms/master/protocol/openid-connect/token
|
@ -0,0 +1,61 @@
|
||||
package com.baeldung.cloud.openfeign;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.baeldung.cloud.openfeign.exception.NotFoundException;
|
||||
import com.baeldung.cloud.openfeign.fileupload.service.UploadService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class OpenFeignFileUploadLiveTest {
|
||||
|
||||
@Autowired
|
||||
private UploadService uploadService;
|
||||
|
||||
private static String FILE_NAME = "fileupload.txt";
|
||||
|
||||
@Test(expected = NotFoundException.class)
|
||||
public void whenFileUploadClientFallbackFactory_thenFileUploadError() throws IOException {
|
||||
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
|
||||
File file = new File(classloader.getResource(FILE_NAME).getFile());
|
||||
Assert.assertTrue(file.exists());
|
||||
FileInputStream input = new FileInputStream(file);
|
||||
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
|
||||
IOUtils.toByteArray(input));
|
||||
uploadService.uploadFileWithFallbackFactory(multipartFile);
|
||||
}
|
||||
|
||||
@Test(expected = NotFoundException.class)
|
||||
public void whenFileUploadClientFallback_thenFileUploadError() throws IOException {
|
||||
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
|
||||
File file = new File(classloader.getResource(FILE_NAME).getFile());
|
||||
Assert.assertTrue(file.exists());
|
||||
FileInputStream input = new FileInputStream(file);
|
||||
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
|
||||
IOUtils.toByteArray(input));
|
||||
uploadService.uploadFileWithFallback(multipartFile);
|
||||
}
|
||||
|
||||
@Test(expected = NotFoundException.class)
|
||||
public void whenFileUploadWithMannualClient_thenFileUploadError() throws IOException {
|
||||
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
|
||||
File file = new File(classloader.getResource(FILE_NAME).getFile());
|
||||
Assert.assertTrue(file.exists());
|
||||
FileInputStream input = new FileInputStream(file);
|
||||
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
|
||||
IOUtils.toByteArray(input));
|
||||
uploadService.uploadFileWithManualClient(multipartFile);
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.baeldung.cloud.openfeign;
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ExampleApplication.class)
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
@ -13,7 +13,7 @@ public class CustomErrorDecoder implements ErrorDecoder {
|
||||
case 400:
|
||||
return new BadRequestException();
|
||||
case 404:
|
||||
return new NotFoundException();
|
||||
return new NotFoundException("Not found !!!");
|
||||
default:
|
||||
return new Exception("Generic error");
|
||||
}
|
||||
|
@ -2,9 +2,6 @@ package com.baeldung.cloud.openfeign.exception;
|
||||
|
||||
public class NotFoundException extends Exception {
|
||||
|
||||
public NotFoundException() {
|
||||
}
|
||||
|
||||
public NotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
@ -15,7 +12,7 @@ public class NotFoundException extends Exception {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NotFoundException: "+getMessage();
|
||||
return "NotFoundException: " + getMessage();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ public class UploadService {
|
||||
|
||||
@Autowired
|
||||
private UploadClient client;
|
||||
|
||||
|
||||
public boolean uploadFileWithManualClient(MultipartFile file) {
|
||||
UploadResource fileUploadResource = Feign.builder().encoder(new SpringFormEncoder())
|
||||
.target(UploadResource.class, HTTP_FILE_UPLOAD_URL);
|
||||
@ -29,5 +29,4 @@ public class UploadService {
|
||||
public String uploadFileError(MultipartFile file) {
|
||||
return client.fileUpload(file);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user