How to call third party API in Spring boot

What you will learn here about Spring Boot

  • How to call third party API in Spring boot

third party api call spring boot
Please click on below link to download how to call third party API in Spring boot sample project

Download sample project to consume 3rd party API (300 downloads)

How to call third party API in Spring boot

Please follow the following steps to know how to call third party API in Spring boot using openfeign.
1)Create simple maven project

2)Add spring boot starter and open feign dependency which is shown below

Spring boot starter web dependency

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Openfeign dependency

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

openfeign dependency spring boot

3)Create response field class (Value class) which is shown below

public class Value {
	private int id;
	private String quote;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

	public String getQuote() {
		return quote;
	}

	public void setQuote(String quote) {
		this.quote = quote;
	}
}

call 3rd party rest api spring boot

4)Create response field class (Quote class) which is shown below

public class Quote {
	private String type;
	private Value value;
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}

	public Value getValue() {
		return value;
	}

	public void setValue(Value value) {
		this.value = value;
	}
}

call 3rd party rest api

5)Enable feign client using @EnableFeignClients annotation in your spring boot application

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

@SpringBootApplication
@EnableFeignClients
public class Application {

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

enable feign clients spring boot

6)Now create proxy interface and define url mapping which is shown below

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name="quote-service", url = "https://quoters.apps.pcfone.io")
public interface Proxy {

	@GetMapping("/api/random")
	Quote getQuote();
}

How to call third party API in Spring boot

7)Create rest controller which is shown below

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class QuoteController {

	@Autowired
	Proxy proxy;
	
	@GetMapping("/quote")
	public Quote getQuote() {
		return proxy.getQuote();
	}
}

3rd party api controller

8)Now please start the application

9)Now go to browser and hit localhost:8080/quote URL which is shown below
Spring boot call third party API in

You may also like...