Declarative REST Client: Feign

Feign is a declarative REST client developed by Netflix. It makes writing REST clients easier by providing a simple API that allows you to define the endpoints you want to call and the parameters you want to pass. Feign automatically generates the HTTP requests and responses, so you don’t have to worry about the low-level details of HTTP. Declarative REST Client: Feign

 

Remove term: Feign makes writing java http clients easier Feign makes writing java http clients

To use Feign, you need to add the following dependency to your project:

Code snippet
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

Once you have added the dependency, you can start defining your endpoints. Each endpoint is defined by an interface that extends FeignClient. The interface must have a method for each endpoint you want to call. The method name must be the same as the endpoint URL. The method parameters must be the same as the parameters required by the endpoint.

For example, the following interface defines an endpoint that calls the /hello endpoint on the http://localhost:8080 server:

Code snippet
@FeignClient("hello")
public interface HelloClient {

    @GetMapping("/hello")
    String hello();

}

Once you have defined your endpoints, you can start using them. To call an endpoint, you need to create an instance of the interface and then call the method. For example, the following code calls the hello() method on the HelloClient:

Code snippet
HelloClient client = new HelloClient();
String greeting = client.hello();

Feign will automatically generate the HTTP request and response, and it will handle the low-level details of HTTP. You can focus on your business logic.

Feign also supports a number of features that can make it easier to use, such as:

  • Interceptors: Interceptors can be used to intercept requests and responses before and after they are sent and received. This can be used to add logging, authentication, or other functionality.
  • Retry: Feign supports retrying failed requests. This can be useful if you are calling a service that is unreliable.
  • Hystrix: Feign integrates with Hystrix, which can be used to provide resilience to failures.

Feign is a powerful tool that can make it easier to write REST clients. It provides a simple API that allows you to define the endpoints you want to call and the parameters you want to pass. Feign automatically generates the HTTP requests and responses, so you don’t have to worry about the low-level details of HTTP.

In this tutorial, we’ll introduce Feign — a declarative HTTP client developed by Netflix.

Feign aims at simplifying HTTP API clients. Simply put, the developer needs only to declare and annotate an interface while the actual implementation is provisioned at runtime.

Feign makes writing java http clients easier.

I have two Service made in Spring boot.

service name is :demoonetomany

Second service :Student service

 

Dependence Of student class which we call data from demoonetomany

 

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:3.1.4'

Feign Client Class

package com.Stundent.management.student.feign;

import com.Stundent.management.student.StudentController.Employ;
import com.Stundent.management.student.StudentController.Post;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@FeignClient(value = "com.onetomany",url = "localhost:8080")
public interface FeignProxy {
    @RequestMapping(method = RequestMethod.GET, value = "/get/post")
    List<Post> getPosts();

    @GetMapping("/list/employ")
     List<Employ> getAllBooks();
}


Controller Of Student Class 

package com.Stundent.management.student.StudentController;

import com.Stundent.management.student.feign.FeignProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class StudentController {
    @Autowired
    FeignProxy feignProxy;
    @RequestMapping(method = RequestMethod.GET, value = "/get/post")
    List<Post> getlistofpost(){
        return feignProxy.getPosts();
    }
    @GetMapping("/list/employ")
    List<Employ> getAllBooks(){
        return feignProxy.getAllBooks();
    }
}

 

Post Entity Class 

package com.Stundent.management.student.StudentController;

public class Post {

    String title;
    String Description;
    String body;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }
}


Employ Class Hold Data

package com.Stundent.management.student.StudentController;

public class Employ {
    String name;
    String status;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}


Main Class Student Class
package com.Stundent.management.student;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class StudentApplication {

   public static void main(String[] args) {
      SpringApplication.run(StudentApplication.class, args);
   }

}

Application Properties 
#spring.datasource.url=jdbc:mysql://localhost:3306/waseem
#spring.datasource.username=root
#spring.datasource.password=
#and other credentials

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/blog1
spring.datasource.username=root
spring.datasource.password=
server.port=8081

demoOnetomany service start

 

Dependence In to demoOnemany Service

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

demoonetomany Controller
package com.onetomany.demoonetomany.API;

import com.onetomany.demoonetomany.Address.Address;
import com.onetomany.demoonetomany.Address.Addressrepository;
import com.onetomany.demoonetomany.Comment.Comment;
import com.onetomany.demoonetomany.Comment.CommentRepository;
import com.onetomany.demoonetomany.Country.Country;
import com.onetomany.demoonetomany.Country.CountryRepository;
import com.onetomany.demoonetomany.Employ.Employ;
import com.onetomany.demoonetomany.Employ.EmployRepository;
import com.onetomany.demoonetomany.Post.Post;
import com.onetomany.demoonetomany.Post.PostRepository;
import com.onetomany.demoonetomany.Request.PostRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class Controller {
    @Autowired
    EmployRepository employRepository;
    @Autowired
    Addressrepository addressrepository;
    @Autowired
    CountryRepository countryRepository;

    @Autowired
    PostRepository postRepository;
  @Autowired
    CommentRepository commentRepository;


    @PostMapping("/add/employ")
    public Employ addemploy(@RequestBody Employ employ){
        List<Country> countryList=new ArrayList<>();
        Country country=new Country();
        country.setStatename("Punjab");
        country.setCode(12340);
        countryList.add(country);

        List<Address> addressList=new ArrayList<>();
        Address address=new Address();
        address.setAddressName("sheikh umad kohna kasur");
        address.setCountry(countryList);
     addressList.add(address);
        Employ employ1=new Employ();
        employ1.setName("Muhammad Waseem haider");
        employ1.setStatus("Mariage");
        employ1.setAddressList(addressList);
    return   employRepository.save(employ1);






    }

    @GetMapping("/list/employ")
    private List<Employ> getAllBooks()
    {
        return employRepository.findAll();
    }

    @PostMapping("/add/post")
    public Post addpost(Post post){

        List<Comment> commentList=new ArrayList<>();

        Comment comment=new Comment();

        comment.setMsg("My name is Muhammad Waseem Haider Software hacker this is post is amazing");
         commentList.add(comment);

        List<Post> postList=new ArrayList<>();

        post.setTitle("how to make a apache server in Spring boot");
        post.setDescription("How to make apache server in spring boot ");
        post.setBody("how to make apache 2 in post data there is 2 option");
        post.setCommentList(commentList);
        postList.add(post);
     return  postRepository.save(post);
    }
    @GetMapping("/get/post")
     List<Post> getallpost(){
        return postRepository.findAll();
    }



}

Post Class Entity
package com.onetomany.demoonetomany.Post;

import com.onetomany.demoonetomany.Comment.Comment;

import javax.persistence.*;
import java.util.List;

@Entity
public class Post {

    @Id
            @GeneratedValue(strategy = GenerationType.AUTO)
    int id;
    String title;
    String Description;
    String body;

    @OneToMany(cascade = CascadeType.ALL)
    List<Comment> commentList;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List<Comment> getCommentList() {
        return commentList;
    }

    public void setCommentList(List<Comment> commentList) {
        this.commentList = commentList;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }
}


Main Class demoontomany

package com.onetomany.demoonetomany;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class DemoonetomanyApplication {

   public static void main(String[] args) {
      SpringApplication.run(DemoonetomanyApplication.class, args);

   }

}

Application properties

#spring.datasource.url=jdbc:mysql://localhost:3306/waseem
#spring.datasource.username=root
#spring.datasource.password=
#and other credentials

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/blog
spring.datasource.username=root
spring.datasource.password=

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *