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:
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.cloud.openfeign.client;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.ObjectFactory;
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.cloud.openfeign.support.SpringEncoder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
import com.baeldung.cloud.openfeign.defaulterrorhandling.model.FormData;
|
||||
|
||||
import feign.codec.Encoder;
|
||||
import feign.form.spring.SpringFormEncoder;
|
||||
|
||||
@FeignClient(name = "form-client", url = "http://localhost:8085/api", configuration = FormFeignEncoderConfig.class)
|
||||
public interface FormClient {
|
||||
|
||||
@PostMapping(value = "/form", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
||||
void postFormData(@RequestBody FormData data);
|
||||
|
||||
@PostMapping(value = "/form/map", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
||||
void postFormMapData(Map<String, ?> data);
|
||||
|
||||
}
|
||||
|
||||
class FormFeignEncoderConfig {
|
||||
|
||||
@Bean
|
||||
public Encoder encoder(ObjectFactory<HttpMessageConverters> converters) {
|
||||
return new SpringFormEncoder(new SpringEncoder(converters));
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.cloud.openfeign.client;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import com.baeldung.cloud.openfeign.config.FeignConfig;
|
||||
|
||||
@FeignClient(name = "user-client", url="https://jsonplaceholder.typicode.com", configuration = FeignConfig.class)
|
||||
public interface UserClient {
|
||||
|
||||
@GetMapping(value = "/users")
|
||||
String getUsers();
|
||||
}
|
||||
+13
@@ -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;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
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.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 {
|
||||
|
||||
@RequestMapping(value = "{id}", method = RequestMethod.GET)
|
||||
Product getProduct(@PathVariable(value = "id") String id);
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.cloud.openfeign.customizederrorhandling.config;
|
||||
|
||||
|
||||
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;
|
||||
|
||||
public class CustomErrorDecoder implements ErrorDecoder {
|
||||
|
||||
@Override
|
||||
public Exception decode(String methodKey, Response response) {
|
||||
switch (response.status()){
|
||||
case 400:
|
||||
return new BadRequestException();
|
||||
case 404:
|
||||
return new ProductNotFoundException("Product not found");
|
||||
case 503:
|
||||
return new ProductServiceNotAvailableException("Product Api is unavailable");
|
||||
default:
|
||||
return new Exception("Exception while getting product details");
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.cloud.openfeign.customizederrorhandling.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import feign.Logger;
|
||||
import feign.codec.ErrorDecoder;
|
||||
|
||||
public class FeignConfig {
|
||||
|
||||
@Bean
|
||||
Logger.Level feignLoggerLevel() {
|
||||
return Logger.Level.FULL;
|
||||
}
|
||||
|
||||
@Bean
|
||||
ErrorDecoder errorDecoder() {
|
||||
return new CustomErrorDecoder();
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.cloud.openfeign.customizederrorhandling.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.cloud.openfeign.customizederrorhandling.client.ProductClient;
|
||||
import com.baeldung.cloud.openfeign.defaulterrorhandling.model.Product;
|
||||
|
||||
@RestController("product_controller2")
|
||||
@RequestMapping(value = "myapp2")
|
||||
public class ProductController {
|
||||
|
||||
private final ProductClient productClient;
|
||||
|
||||
@Autowired
|
||||
public ProductController(ProductClient productClient) {
|
||||
this.productClient = productClient;
|
||||
}
|
||||
|
||||
@GetMapping("/product/{id}")
|
||||
public Product getProduct(@PathVariable String id) {
|
||||
return productClient.getProduct(id);
|
||||
}
|
||||
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.cloud.openfeign.customizederrorhandling.exception;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class ErrorResponse {
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
|
||||
private Date timestamp;
|
||||
|
||||
@JsonProperty(value = "code")
|
||||
private int code;
|
||||
|
||||
@JsonProperty(value = "status")
|
||||
private String status;
|
||||
@JsonProperty(value = "message")
|
||||
private String message;
|
||||
@JsonProperty(value = "details")
|
||||
private String details;
|
||||
|
||||
public ErrorResponse() {
|
||||
}
|
||||
|
||||
public ErrorResponse(HttpStatus httpStatus, String message, String details) {
|
||||
timestamp = new Date();
|
||||
this.code = httpStatus.value();
|
||||
this.status = httpStatus.name();
|
||||
this.message = message;
|
||||
this.details = details;
|
||||
}
|
||||
|
||||
public Date getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public String getDetails() {
|
||||
return details;
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.cloud.openfeign.customizederrorhandling.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class ProductExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
@ExceptionHandler({ProductServiceNotAvailableException.class})
|
||||
public ResponseEntity<ErrorResponse> handleProductServiceNotAvailableException(ProductServiceNotAvailableException exception, WebRequest request) {
|
||||
return new ResponseEntity<>(new ErrorResponse(
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
exception.getMessage(),
|
||||
request.getDescription(false)),
|
||||
HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
@ExceptionHandler({ProductNotFoundException.class})
|
||||
public ResponseEntity<ErrorResponse> handleProductNotFoundException(ProductNotFoundException exception, WebRequest request) {
|
||||
return new ResponseEntity<>(new ErrorResponse(
|
||||
HttpStatus.NOT_FOUND,
|
||||
exception.getMessage(),
|
||||
request.getDescription(false)),
|
||||
HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ErrorResponse> handleGenericException(Exception exception, WebRequest request) {
|
||||
return new ResponseEntity<>(new ErrorResponse(
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
exception.getMessage(),
|
||||
request.getDescription(false)),
|
||||
HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.cloud.openfeign.customizederrorhandling.exception;
|
||||
|
||||
public class ProductNotFoundException extends RuntimeException {
|
||||
|
||||
public ProductNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.cloud.openfeign.customizederrorhandling.exception;
|
||||
|
||||
public class ProductServiceNotAvailableException extends RuntimeException {
|
||||
|
||||
public ProductServiceNotAvailableException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
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.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 {
|
||||
|
||||
@RequestMapping(value = "{id}", method = RequestMethod.GET)
|
||||
Product getProduct(@PathVariable(value = "id") String id);
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.cloud.openfeign.defaulterrorhandling.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import feign.Logger;
|
||||
|
||||
public class FeignConfig {
|
||||
|
||||
@Bean
|
||||
Logger.Level feignLoggerLevel() {
|
||||
return Logger.Level.FULL;
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.cloud.openfeign.defaulterrorhandling.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.cloud.openfeign.defaulterrorhandling.client.ProductClient;
|
||||
import com.baeldung.cloud.openfeign.defaulterrorhandling.model.Product;
|
||||
|
||||
@RestController("product_controller1")
|
||||
@RequestMapping(value ="myapp1")
|
||||
public class ProductController {
|
||||
|
||||
private final ProductClient productClient;
|
||||
|
||||
@Autowired
|
||||
public ProductController(ProductClient productClient) {
|
||||
this.productClient = productClient;
|
||||
}
|
||||
|
||||
@GetMapping("/product/{id}")
|
||||
public Product getProduct(@PathVariable String id) {
|
||||
return productClient.getProduct(id);
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.cloud.openfeign.defaulterrorhandling.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class FormData {
|
||||
|
||||
int id;
|
||||
String name;
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.cloud.openfeign.defaulterrorhandling.model;
|
||||
|
||||
public class Product {
|
||||
|
||||
private String id;
|
||||
|
||||
private String productName;
|
||||
|
||||
private double price;
|
||||
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getProductName() {
|
||||
return productName;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-7
@@ -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
|
||||
|
||||
+2
-1
@@ -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();
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.cloud.openfeign.fileupload.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
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.fileupload.service.UploadService;
|
||||
|
||||
@RestController
|
||||
public class FileController {
|
||||
|
||||
@Autowired
|
||||
private UploadService service;
|
||||
|
||||
@PostMapping(value = "/upload")
|
||||
public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
|
||||
return service.uploadFileWithFallbackFactory(file);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/upload-mannual-client")
|
||||
public boolean handleFileUploadWithManualClient(
|
||||
@RequestPart(value = "file") MultipartFile file) {
|
||||
return service.uploadFileWithManualClient(file);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/upload-error")
|
||||
public String handleFileUploadError(@RequestPart(value = "file") MultipartFile file) {
|
||||
return service.uploadFileWithFallbackFactory(file);
|
||||
}
|
||||
|
||||
}
|
||||
+10
-13
@@ -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!!!";
|
||||
};
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
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)
|
||||
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);
|
||||
}
|
||||
+1
-1
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user