Declarative REST Client: Feign

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.