Spring Boot – Using the Business Service

We would learn about how to use the business services we created previously. We actually will move the logic from the controller to the business service.

 

  1. How Business Service Work
  2. Adding Dependency to the Controller
  3. The UserService and Controller
  4. The PostService and Controller
  5. The Location Service and Controller

 

1. How Business Service Work

Remember that we already wrote a method to get items. For instance, getAllUsers returns list of all the users. In the same way, getAllLocations returns list of all location. We wrote these methods in the controller file. The fact is that we actually need to get these information from the business service.Business Service is also called Data Access Layer.

So if we need a list of users, we ask the business service to provide it. Also if we need a list of locations, we simply ask the business service and so on.

This then means that the controller should actually be talking to the business service.

However all this is made possible by Dependency Injection.

 

 

2. Add the Dependencies to the Controllers 

We need to enable dependency injection in our controller files.  To to that, we take two steps:

  • Create a private member variable in the controller class
  • Add the @Autowired annotation to this member variable
  • The move the get methods over to the business service

So when application starts, a new instance of the business service is created and registered. Then when a controller is created, Spring checks if there is an @Autowired annotation. If yes, then it takes the existing instance of the business service and injects it into the controller.

 

 

3. The User Service and Controller

After adding the dependency injection to the UserController and moving the getAllUser() method to the UserService, the the User controller would be as below:

 

package com.kindsonthegenius.social.user;

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

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

import com.kindsonthegenius.social.location.Location;

@RestController
public class userController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/users")
	public List<User> getAllUsers() {

		return userService.getAllUsers();	
	}
}

Listing 1.0: The UserController Class

 

Similarly, the userService would now contain the definition for list of Users as shown below

 

package com.kindsonthegenius.social.user;

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

import org.springframework.stereotype.Service;

import com.kindsonthegenius.social.location.Location;

@Service
public class UserService {
	
   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");
	
   private List<User> users = Arrays.asList(user1, user2);	
	
   public List<User> getAllUsers() {
	return users;
   }
}

Listing 1.1: The UserService Class

 

Test the Application

To test the application, right-click on the the project and choose Run As > Spring Boot Application

Then visit localhost:8000/users

You will get a list of users in the json format. Now, everything work fine!

 

 

4. The Post Service and Controller

Follow the same method to adjust the PostService and PostController files. The PostController File would be as shown below:

 

package com.kindsonthegenius.social.post;

import java.util.List;

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

@RestController
public class PostController {
	
	@Autowired
	private PostService postService;
	
	@RequestMapping(value = "/posts")
	public List<Post> getAllPosts() {
		
		return postService.getAllPosts();
	}
}

Listing 1.2: Content of the PostConroller file

 

Similarly, the content of PostService would be as shown below

 

package com.kindsonthegenius.social.post;

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

import org.springframework.stereotype.Service;

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

@Service
public class PostService {

   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");
	
   List<Post> posts = Arrays.asList(post1, post2);
	
   public List<Post> getAllPosts() {
	return posts;
   }
	
}

Listing 1.3: Content of the PostService class

 

 

5. The Location Service and Controller

Finally, modify the LocationService and Location Controller files. You LocationController class would then be as shown below:

 

package com.kindsonthegenius.social.location;

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

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

@RestController
public class LocationController {
	
   @Autowired
   private LocationService locationService;
	
   @RequestMapping(value = "/locations")
   public List<Location> getAllLocations() 
   {
	return locationService.getAllLocations();
   }		

}

Listing 1.4: Content of the LocationController class

 

Similarly, the content of LocationService would be as shown below

 

package com.kindsonthegenius.social.location;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

}

Listing 1.5: Content of the LocationService class

 

Finally, go ahead to test the application.Access the following urls:

http://localhost:8080/users

http://localhost:8080/posts

http://localhost:8080/locations