Spring Boot – Write GET Methods

In this lesson, we are going to write the GET methods to get list of items. So we would write methods to return list of users. Then we would write method to return list of Posts. Finally, we write method to return list of Locations.

We’ll cover the following:

  1. Write and Test a GET Method
  2. Write the Method to return List of Users
  3. Write the Method to return List of Posts
  4. Write the Method to return List of Locations

 

1. Write and Test a GET Method

So we would first write a GET method. This method would just return a string “Welcome to Spring Boot” when the someone accesses /welcome. To do this, create a new class and name it welcomeController.

Inside this file write a function that write  function welcome() that returns a string “Welcome to Spring Boot”

Annotate the class with @RestConroller annotation

Annotate the function with @RestMapping annotation

If you have done everything right, the content of you welcomeController file would be as shown below.

 

package com.kindsonthegenius.social;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class welcomeController {
	
	@RequestMapping(value="/welcome")
	public static String welcome() {
		return "Welcome to Spring Boot";
	}
	
}

Listing 1.0: Content of the welcomeController.java

 

Now you can start the application. Then open the browser and enter http://localhost:8080/welcome

 

2. Write the Method to return List of Users

Here we will write the function that would return list of all the users in our SocialAPI.

Open the UserController file you created in the previous tutorial. Inside this file you will do the following things:

create a function and name if getAllUsers()

annotate this function with @RestMapping annotation

inside this function create a hard-coded list of users

 

package com.kindsonthegenius.social.user;

import java.util.Arrays;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kindsonthegenius.social.location.Location;

@RestController
public class userController {
	
   @RequestMapping(value="/users")
   public List<User> getAllUsers() {
	User user1 = new User(
		"u1", 
		"Jany", 
		"Lawrence",
		new Location("l1", "Lagos"),
		"Jany@gmail.com");
		
	User user2 = new User(
		"u2", 
		"Jadon", 
		"Mills",
		new Location("l2", "Asaba"),
		"Jadon@gmail.com");
		
	return Arrays.asList(user1, user2);	
	
   }
}

Listing 1.1: Content of the userController.java file

 

Now you can test the application.

Right-click on the project and Run As SpringBoot Application. Then go to the broser and access http://localhost:8080/users. You will get a list of users.

If not let me know in the comment box below.

 

3. Write the Method to return List of Posts

Here we will write the function that would return list of all the posts in our SocialAPI.

Open the PostController file you created in the previous tutorial. Inside this file you will do the following things:

create a function and name if getAllPosts()

annotate this function with @RestMapping annotation. See the video

inside this function create a hard-coded list of posts

 

package com.kindsonthegenius.social.post;

import java.util.Arrays;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kindsonthegenius.social.location.Location;
import com.kindsonthegenius.social.user.User;

@RestController
public class PostController {
	
   @RequestMapping(value = "/posts")
   public List<Post> getAllPosts() {
		
	User user1 = new User(
		"u1", 
		"Jany", 
		"Lawrence",
		new Location("l1", "Lagos"),
		"Jany@gmail.com");
		
	User user2 = new User(
		"u2", 
		"Jadon", 
		"Mills",
		new Location("l2", "Asaba"),
		"Jadon@gmail.com");
		
	Post post1 = new Post(
		"p1",
		"01-Jan-19",
		user1,
	        "Its good to love and be loved");
		
	Post post2 = new Post(
		"p2",
	        "02-Jan-19",
		user2,
		"We all need someone");		
		
	return Arrays.asList(post1, post2);
 
   }
}

Listing 1.2: Content of the postController.java file

 

Now you can test the getAllPosts method.

Right-click on the project and Run As SpringBoot Application. Then go to the browser and access http://localhost:8080/posts. You will get a list of posts.

If not let me know in the comment box below.

 

4. Write the Method to return List of Locations

Here we will write the function that would return list of all the posts in our SocialAPI. This would be easier than the previous two.

Open the LocationController file you created in the previous tutorial. Inside this file you will do the following things:

create a function and name if getAllLocations()

annotate this function with @RestMapping annotation. See the video

inside this function create a hard-coded list of locations

 

import java.util.Arrays;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LocationController {
	
   @RequestMapping(value = "/locations")
   public List<Location> getAllLocations() 
   {
	Location location1 = new Location("l1", "Lagos");
	Location location2 = new Location("l2", "Asaba");
	Location location3 = new Location("l3", "Budapest");	
		
	return Arrays.asList(location1, location2, location3);
   }		

}

Listing 1.3: Content of the LocationController.java file

 

As usual, go ahead to test the getAllLocations method.

If you completed this part successfully, then thumbs up to you! Next we would see how to write POST methods.