Menu

1 / 2

Consuming RESTful API services using Rest Template

DEV Community·Ayush Singh·6 months ago
#Y9WLykdQ
#for#api#backend#java#name
Reading 0:00
15s threshold

One of most important and basic operations in Java Backend is Consuming Rest api. In this blog I use REST Template to demonstrate the task.

GET Method

You have to create 2 projects, from one demo we use other's get endpoint.

For 1st Project, which only has the name[Your name].

You have to add spring-web dependency for web services.

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

java

Create a MainController class in the controller package.

Add @RestController annotation to make it RESTful web services and instruct it to return the data instead of view.

Add a Get endpoint return your name.

package com.example.demo.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;

@RestController public class MainController {

@GetMapping("/name") public String myName() { return "Ayush Singh"; } }

Enter fullscreen mode

Exit fullscreen mode

Add a variable in…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.