This commit is contained in:
Dhananjay Singh
2018-11-24 22:15:10 +05:30
parent 2c8ba7fb97
commit 78f3ac1c32
31 changed files with 626 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
package com.javadevjournal.feignclientexample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AppController {
@Autowired
ProductServiceClient productServiceClient;
@GetMapping("/fetchProducts")
public ResponseEntity<?> fetchProducts() {
return ResponseEntity.ok(productServiceClient.getAllProducts());
}
@GetMapping("/fetchProduct/{id}")
public ResponseEntity<?> fetchProduct(@PathVariable int id) {
Product product = productServiceClient.getProduct(id);
return ResponseEntity.ok(product);
}
}

View File

@@ -0,0 +1,16 @@
package com.javadevjournal.feignclientexample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignClientExampleApplication {
public static void main(String[] args) {
SpringApplication.run(FeignClientExampleApplication.class, args);
}
}

View File

@@ -0,0 +1,28 @@
package com.javadevjournal.feignclientexample;
public class Product {
private Integer id;
private String name;
private double price;
public Product(Integer id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}

View File

@@ -0,0 +1,19 @@
package com.javadevjournal.feignclientexample;
import java.util.List;
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;
@FeignClient(name = "product-service"/*, url = "http://localhost:8081"*/)
public interface ProductServiceClient {
@RequestMapping(value = "/products", method = RequestMethod.GET)
public List<Product> getAllProducts();
@RequestMapping(value = "/product/{id}", method = RequestMethod.GET)
public Product getProduct(@PathVariable("id") int productId);
}

View File

@@ -0,0 +1,19 @@
package com.javadevjournal.feignclientexample.errors;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class AppExceptionHandler {
@ResponseBody
@ExceptionHandler(value = ProductNotFoundException.class)
public ResponseEntity<?> handleException(ProductNotFoundException exception) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(exception.getMessage());
}
}

View File

@@ -0,0 +1,22 @@
package com.javadevjournal.feignclientexample.errors;
import org.springframework.stereotype.Component;
import feign.Response;
import feign.codec.ErrorDecoder;
@Component
public class AppFeignErrorDecoder implements ErrorDecoder {
private final ErrorDecoder defaultErrorDecoder = new Default();
@Override
public Exception decode(String methodKey, Response response) {
if (response.status() >= 400 && response.status() <= 499) {
throw new ProductNotFoundException("Product Not Found");
}
return defaultErrorDecoder.decode(methodKey, response);
}
}

View File

@@ -0,0 +1,10 @@
package com.javadevjournal.feignclientexample.errors;
public class ProductNotFoundException extends RuntimeException {
private static final long serialVersionUID = 1L;
public ProductNotFoundException(String msg) {
super(msg);
}
}

View File

@@ -0,0 +1,2 @@
server.port=8080
spring.application.name=feign-client-example

View File

@@ -0,0 +1,16 @@
package com.javadevjournal.feignclient;
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
public class FeignClientApplicationTests {
@Test
public void contextLoads() {
}
}