JAVA-17818 Split or move spring-cloud-openfeign module (conti-2) (#13621)
* JAVA-17818 Split or move spring-cloud-openfeign module (conti-2) * JAVA-17818 Some adjustments in the README.md file --------- Co-authored-by: timis1 <noreplay@yahoo.com>
This commit is contained in:
parent
9af9ee9b48
commit
9c571d3424
|
@ -6,10 +6,4 @@ This module contains articles about Feign
|
|||
|
||||
- [Intro to Feign](https://www.baeldung.com/intro-to-feign)
|
||||
- [Retrying Feign Calls](https://www.baeldung.com/feign-retry)
|
||||
- [Setting Request Headers Using Feign](https://www.baeldung.com/java-feign-request-headers)
|
||||
- [File Upload With Open Feign](https://www.baeldung.com/java-feign-file-upload)
|
||||
- [Feign Logging Configuration](https://www.baeldung.com/java-feign-logging)
|
||||
- [Retrieve Original Message From Feign ErrorDecoder](https://www.baeldung.com/feign-retrieve-original-message)
|
||||
- [RequestLine with Feign Client](https://www.baeldung.com/feign-requestline)
|
||||
- [Propagating Exceptions With OpenFeign and Spring](https://www.baeldung.com/spring-openfeign-propagate-exception)
|
||||
- [Post form-url-encoded Data with Spring Cloud Feign](https://www.baeldung.com/spring-cloud-post-form-url-encoded-data)
|
||||
- [Setting Request Headers Using Feign](https://www.baeldung.com/java-feign-request-headers)
|
|
@ -69,11 +69,6 @@
|
|||
<artifactId>feign-form-spring</artifactId>
|
||||
<version>${feign.form.spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
<version>${spring.cloud.openfeign.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.tomakehurst</groupId>
|
||||
<artifactId>wiremock-jre8</artifactId>
|
||||
|
|
|
@ -2,10 +2,8 @@ package com.baeldung.core;
|
|||
|
||||
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) {
|
||||
|
|
|
@ -1,55 +0,0 @@
|
|||
package com.baeldung.core.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 + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
package com.baeldung.core.fileupload.config;
|
||||
|
||||
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 HttpMessageConverters(new RestTemplate().getMessageConverters())));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ErrorDecoder errorDecoder() {
|
||||
return new RetreiveMessageErrorDecoder();
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
package com.baeldung.core.fileupload.config;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import com.baeldung.core.exception.BadRequestException;
|
||||
import com.baeldung.core.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;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
package com.baeldung.core.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-file")
|
||||
@Headers("Content-Type: multipart/form-data")
|
||||
Response uploadFile(@Param("file") MultipartFile file);
|
||||
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
package com.baeldung.core.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 UploadClient client;
|
||||
|
||||
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 uploadFile(MultipartFile file) {
|
||||
return client.fileUpload(file);
|
||||
}
|
||||
|
||||
public String uploadFileError(MultipartFile file) {
|
||||
return client.fileUpload(file);
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
package com.baeldung.core;
|
||||
|
||||
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.core.fileupload.service.UploadService;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ExampleApplication.class)
|
||||
public class OpenFeignFileUploadLiveTest {
|
||||
|
||||
@Autowired
|
||||
private UploadService uploadService;
|
||||
|
||||
private static String FILE_NAME = "fileupload.txt";
|
||||
|
||||
@Test
|
||||
public void whenFeignBuilder_thenFileUploadSuccess() 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));
|
||||
Assert.assertTrue(uploadService.uploadFileWithManualClient(multipartFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAnnotatedFeignClient_thenFileUploadSuccess() 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));
|
||||
String uploadFile = uploadService.uploadFile(multipartFile);
|
||||
Assert.assertNotNull(uploadFile);
|
||||
}
|
||||
}
|
|
@ -5,4 +5,10 @@
|
|||
- [Provide an OAuth2 Token to a Feign Client](https://www.baeldung.com/spring-cloud-feign-oauth-token)
|
||||
- [Propagating Exceptions With OpenFeign and Spring](https://www.baeldung.com/spring-openfeign-propagate-exception)
|
||||
- [Feign Client Exception Handling](https://www.baeldung.com/java-feign-client-exception-handling)
|
||||
- [File Upload With Open Feign](https://www.baeldung.com/java-feign-file-upload)
|
||||
- [Feign Logging Configuration](https://www.baeldung.com/java-feign-logging)
|
||||
- [Retrieve Original Message From Feign ErrorDecoder](https://www.baeldung.com/feign-retrieve-original-message)
|
||||
- [RequestLine with Feign Client](https://www.baeldung.com/feign-requestline)
|
||||
- [Propagating Exceptions With OpenFeign and Spring](https://www.baeldung.com/spring-openfeign-propagate-exception)
|
||||
- [Post form-url-encoded Data with Spring Cloud Feign](https://www.baeldung.com/spring-cloud-post-form-url-encoded-data)
|
||||
- [Configuring Spring Cloud FeignClient URL](https://www.baeldung.com/spring-cloud-feignclient-url)
|
||||
|
|
|
@ -61,10 +61,23 @@
|
|||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.tomakehurst</groupId>
|
||||
<artifactId>wiremock-jre8</artifactId>
|
||||
<version>${wire.mock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring-cloud.version>2021.0.3</spring-cloud.version>
|
||||
<wire.mock.version>2.33.2</wire.mock.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.client;
|
||||
package com.baeldung.cloud.openfeign.client;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -11,7 +11,7 @@ import org.springframework.http.MediaType;
|
|||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
import com.baeldung.core.defaulterrorhandling.model.FormData;
|
||||
import com.baeldung.cloud.openfeign.defaulterrorhandling.model.FormData;
|
||||
|
||||
import feign.codec.Encoder;
|
||||
import feign.form.spring.SpringFormEncoder;
|
|
@ -1,9 +1,9 @@
|
|||
package com.baeldung.core.client;
|
||||
package com.baeldung.cloud.openfeign.client;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import com.baeldung.core.config.FeignConfig;
|
||||
import com.baeldung.cloud.openfeign.config.FeignConfig;
|
||||
|
||||
@FeignClient(name = "user-client", url="https://jsonplaceholder.typicode.com", configuration = FeignConfig.class)
|
||||
public interface UserClient {
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.cloud.openfeign.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import feign.Logger;
|
||||
|
||||
public class FeignConfig {
|
||||
|
||||
@Bean
|
||||
Logger.Level feignLoggerLevel() {
|
||||
return Logger.Level.FULL;
|
||||
}
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
package com.baeldung.core.customizederrorhandling.client;
|
||||
package com.baeldung.cloud.openfeign.customizederrorhandling.client;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import com.baeldung.core.customizederrorhandling.config.FeignConfig;
|
||||
import com.baeldung.core.defaulterrorhandling.model.Product;
|
||||
import com.baeldung.cloud.openfeign.customizederrorhandling.config.FeignConfig;
|
||||
import com.baeldung.cloud.openfeign.defaulterrorhandling.model.Product;
|
||||
|
||||
@FeignClient(name = "product-client-2", url = "http://localhost:8081/product/", configuration = FeignConfig.class)
|
||||
public interface ProductClient {
|
|
@ -1,8 +1,9 @@
|
|||
package com.baeldung.core.customizederrorhandling.config;
|
||||
package com.baeldung.cloud.openfeign.customizederrorhandling.config;
|
||||
|
||||
import com.baeldung.core.customizederrorhandling.exception.ProductNotFoundException;
|
||||
import com.baeldung.core.customizederrorhandling.exception.ProductServiceNotAvailableException;
|
||||
import com.baeldung.core.exception.BadRequestException;
|
||||
|
||||
import com.baeldung.cloud.openfeign.customizederrorhandling.exception.ProductNotFoundException;
|
||||
import com.baeldung.cloud.openfeign.customizederrorhandling.exception.ProductServiceNotAvailableException;
|
||||
import com.baeldung.cloud.openfeign.exception.BadRequestException;
|
||||
|
||||
import feign.Response;
|
||||
import feign.codec.ErrorDecoder;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.customizederrorhandling.config;
|
||||
package com.baeldung.cloud.openfeign.customizederrorhandling.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.customizederrorhandling.controller;
|
||||
package com.baeldung.cloud.openfeign.customizederrorhandling.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
@ -6,8 +6,8 @@ import org.springframework.web.bind.annotation.PathVariable;
|
|||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.core.customizederrorhandling.client.ProductClient;
|
||||
import com.baeldung.core.defaulterrorhandling.model.Product;
|
||||
import com.baeldung.cloud.openfeign.customizederrorhandling.client.ProductClient;
|
||||
import com.baeldung.cloud.openfeign.defaulterrorhandling.model.Product;
|
||||
|
||||
@RestController("product_controller2")
|
||||
@RequestMapping(value = "myapp2")
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.customizederrorhandling.exception;
|
||||
package com.baeldung.cloud.openfeign.customizederrorhandling.exception;
|
||||
|
||||
import java.util.Date;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.customizederrorhandling.exception;
|
||||
package com.baeldung.cloud.openfeign.customizederrorhandling.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.customizederrorhandling.exception;
|
||||
package com.baeldung.cloud.openfeign.customizederrorhandling.exception;
|
||||
|
||||
public class ProductNotFoundException extends RuntimeException {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.customizederrorhandling.exception;
|
||||
package com.baeldung.cloud.openfeign.customizederrorhandling.exception;
|
||||
|
||||
public class ProductServiceNotAvailableException extends RuntimeException {
|
||||
|
|
@ -1,12 +1,12 @@
|
|||
package com.baeldung.core.defaulterrorhandling.client;
|
||||
package com.baeldung.cloud.openfeign.defaulterrorhandling.client;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import com.baeldung.core.defaulterrorhandling.config.FeignConfig;
|
||||
import com.baeldung.core.defaulterrorhandling.model.Product;
|
||||
import com.baeldung.cloud.openfeign.defaulterrorhandling.config.FeignConfig;
|
||||
import com.baeldung.cloud.openfeign.defaulterrorhandling.model.Product;
|
||||
|
||||
@FeignClient(name = "product-client", url = "http://localhost:8084/product/", configuration = FeignConfig.class)
|
||||
public interface ProductClient {
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.defaulterrorhandling.config;
|
||||
package com.baeldung.cloud.openfeign.defaulterrorhandling.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.defaulterrorhandling.controller;
|
||||
package com.baeldung.cloud.openfeign.defaulterrorhandling.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
@ -6,8 +6,8 @@ import org.springframework.web.bind.annotation.PathVariable;
|
|||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.core.defaulterrorhandling.client.ProductClient;
|
||||
import com.baeldung.core.defaulterrorhandling.model.Product;
|
||||
import com.baeldung.cloud.openfeign.defaulterrorhandling.client.ProductClient;
|
||||
import com.baeldung.cloud.openfeign.defaulterrorhandling.model.Product;
|
||||
|
||||
@RestController("product_controller1")
|
||||
@RequestMapping(value ="myapp1")
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.defaulterrorhandling.model;
|
||||
package com.baeldung.cloud.openfeign.defaulterrorhandling.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.defaulterrorhandling.model;
|
||||
package com.baeldung.cloud.openfeign.defaulterrorhandling.model;
|
||||
|
||||
public class Product {
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
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;
|
||||
|
@ -13,12 +12,7 @@ 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());
|
||||
}
|
||||
}));
|
||||
return new SpringFormEncoder(new SpringEncoder(() -> new HttpMessageConverters(new RestTemplate().getMessageConverters())));
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
|
|
@ -12,9 +12,10 @@ 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;
|
||||
ExceptionMessage message;
|
||||
try (InputStream bodyIs = response.body()
|
||||
.asInputStream()) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.fileupload.controller;
|
||||
package com.baeldung.cloud.openfeign.fileupload.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
@ -6,7 +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.core.fileupload.service.UploadService;
|
||||
import com.baeldung.cloud.openfeign.fileupload.service.UploadService;
|
||||
|
||||
@RestController
|
||||
public class FileController {
|
||||
|
@ -16,7 +16,7 @@ public class FileController {
|
|||
|
||||
@PostMapping(value = "/upload")
|
||||
public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
|
||||
return service.uploadFile(file);
|
||||
return service.uploadFileWithFallbackFactory(file);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/upload-mannual-client")
|
||||
|
@ -27,7 +27,7 @@ public class FileController {
|
|||
|
||||
@PostMapping(value = "/upload-error")
|
||||
public String handleFileUploadError(@RequestPart(value = "file") MultipartFile file) {
|
||||
return service.uploadFile(file);
|
||||
return service.uploadFileWithFallbackFactory(file);
|
||||
}
|
||||
|
||||
}
|
|
@ -11,20 +11,17 @@ import com.baeldung.cloud.openfeign.exception.NotFoundException;
|
|||
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!!!";
|
||||
return 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!!!";
|
||||
};
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.fileupload.service;
|
||||
package com.baeldung.cloud.openfeign.fileupload.service;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.http.MediaType;
|
||||
|
@ -6,7 +6,7 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.baeldung.core.fileupload.config.FeignSupportConfig;
|
||||
import com.baeldung.cloud.openfeign.fileupload.config.FeignSupportConfig;
|
||||
|
||||
@FeignClient(name = "file", url = "http://localhost:8081", configuration = FeignSupportConfig.class)
|
||||
public interface UploadClient {
|
|
@ -9,7 +9,7 @@ import feign.Response;
|
|||
|
||||
public interface UploadResource {
|
||||
|
||||
@RequestLine("POST /upload-error")
|
||||
@RequestLine("POST /upload-file")
|
||||
@Headers("Content-Type: multipart/form-data")
|
||||
Response uploadFile(@Param("file") MultipartFile file);
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.client;
|
||||
package com.baeldung.cloud.openfeign.client;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
|
||||
|
@ -20,7 +20,7 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import com.baeldung.core.defaulterrorhandling.model.FormData;
|
||||
import com.baeldung.cloud.openfeign.defaulterrorhandling.model.FormData;
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
import com.github.tomakehurst.wiremock.client.WireMock;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.customizederrorhandling.client;
|
||||
package com.baeldung.cloud.openfeign.customizederrorhandling.client;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
|
||||
|
@ -15,9 +15,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.core.ExampleApplication;
|
||||
import com.baeldung.core.customizederrorhandling.exception.ProductNotFoundException;
|
||||
import com.baeldung.core.customizederrorhandling.exception.ProductServiceNotAvailableException;
|
||||
import com.baeldung.cloud.openfeign.ExampleApplication;
|
||||
import com.baeldung.cloud.openfeign.customizederrorhandling.exception.ProductNotFoundException;
|
||||
import com.baeldung.cloud.openfeign.customizederrorhandling.exception.ProductServiceNotAvailableException;
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.customizederrorhandling.controller;
|
||||
package com.baeldung.cloud.openfeign.customizederrorhandling.controller;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
|
||||
|
@ -21,8 +21,8 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
|
||||
import com.baeldung.core.customizederrorhandling.client.ProductClient;
|
||||
import com.baeldung.core.customizederrorhandling.exception.ErrorResponse;
|
||||
import com.baeldung.cloud.openfeign.customizederrorhandling.client.ProductClient;
|
||||
import com.baeldung.cloud.openfeign.customizederrorhandling.exception.ErrorResponse;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
import com.github.tomakehurst.wiremock.client.WireMock;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.defaulterrorhandling.client;
|
||||
package com.baeldung.cloud.openfeign.defaulterrorhandling.client;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
|
||||
|
@ -17,8 +17,8 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.core.ExampleApplication;
|
||||
import com.baeldung.core.defaulterrorhandling.model.Product;
|
||||
import com.baeldung.cloud.openfeign.ExampleApplication;
|
||||
import com.baeldung.cloud.openfeign.defaulterrorhandling.model.Product;
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
|
||||
import feign.FeignException;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.defaulterrorhandling.controller;
|
||||
package com.baeldung.cloud.openfeign.defaulterrorhandling.controller;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
|
||||
|
@ -21,7 +21,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import com.baeldung.core.defaulterrorhandling.client.ProductClient;
|
||||
import com.baeldung.cloud.openfeign.defaulterrorhandling.client.ProductClient;
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
import com.github.tomakehurst.wiremock.client.WireMock;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.core.defaulterrorhandling.controller;
|
||||
package com.baeldung.cloud.openfeign.defaulterrorhandling.controller;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
Loading…
Reference in New Issue