Implementing microservices using Spring Boot

A microservice can be implemented in many ways; in the previous section, we saw one way to implement it, using WildFly Swarm, which is a MicroProfile implementation. In this section, we will see how to implement a microservice using Spring Boot, which is not a MicroProfile implementation but is a popular framework.

Spring Boot (https://spring.io/projects/spring-boot/) is a framework to create standalone Spring applications. Refer to Chapter 8Creating Web Applications with Spring MVC, for more information on Spring and specific information on the Spring MVC framework. Similar to the WildFly Swarm Project Generator, Spring Boot also has a web page for creating a starter application for Spring Boot, where you can select the features/specifications of JEE that you want to be included in the application. Go to https://start.spring.io/:

Figure 12.6: Spring Boot project generator

Select the Web, JPA, and Jersey(JAX-RS) dependencies. Download the starter project and unzip it in a folder. We won’t be able to run the application yet. Since we have selected JPA as one of the dependencies of the application, Spring Boot expects us to configure database connection properties in the application.properties file, located in src/main/resources. Add the following properties to application.properties:

spring.datasource.url = jdbc:mysql://localhost/course_management?autoReconnect=true&useSSL=false
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.username=<your_user_name>
spring.datasource.password=<your_passwod>
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

We can run the server now, but we haven’t defined any REST endpoints yet. So, let’s do that. We will use the Course entity bean that we created for the WildFly Swarm project in the previous section. So, copy the same file to this project, in the packt.book.jeeeclipse.springboot.coursemanagementspring package. See the Create course entity bean and JPA factory section for listings of the Course class.

Spring provides a utility interface named CrudRepository that tells the framework to create CRUD boilerplate code for the given entity/class. We will create a repository interface that extends CrudRepository and create a CRUD implementation for the Course class. See https://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html for more information on CrudRepository:

package packt.book.jeeeclipse.springboot.coursemanagementspring;
import org.springframework.data.repository.CrudRepository;
public interface CourseRepository extends CrudRepository<Course, Long>{
}

This is just a marker interface to tell Spring Framework to create CRUD code for the Course class/entity that has the primary key of type Long.

In Spring, a REST endpoint is created by creating a controller, actually annotating the class with @RestController. See https://spring.io/guides/gs/rest-service/ for information on creating RESTful web services using Spring. So, let’s create the CourseController class:

package com.example.demo;
// skipping imports to save space

@RestController
public class CourseController {
@Autowired
private CourseRepository courseRepository;

@RequestMapping(value = "/course_management/courses", method = RequestMethod.GET)
public Iterable<Course> getCourses() {
return courseRepository.findAll();
}
}

In this class, we are mapping the GET HTTP request to the /course_management/courses URL to the getCourses method.

An instance of CourseRepository is auto injected into this class using the @Autowired annotation.

We are now ready to run the application. Create a run configuration for this application by right-clicking on the project in Project Explorer and selecting Run As | Maven Build. Then, type spring-boot:run in the Goals field ( see Figure 12.4 for reference) and click the Run button. Once the server is ready, browse to http://localhost:8080/course_management/courses and you should see JSON output (for Courses).

To change the default server port from 8080 to any other port number, say 8000, set the environment variable server.port=8000. See Figure 12.5 for reference.

See https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/ for a complete reference to Spring Boot.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.190.211