- Spring 5.0 Microservices(Second Edition)
- Rajesh R V
- 1079字
- 2021-07-02 19:45:01
Developing our first Spring Boot microservice
In this section, we will demonstrate how to develop a Java-based REST/JSON Spring Boot service using STS.
- Open STS, right-click in Project Explorer window, select New Project, then select Spring Starter Project as shown in the following screenshot. Then click on Next:

- The Spring Starter Project is a basic template wizard, which provides a selection of a number of other starter libraries.
- Type the project name as chapter3.bootrest, or any other name of your choice. It is important to choose the packaging as Jar. In traditional web applications, a war file is created, and then deployed into a servlet container, whereas, Spring Boot packages all the dependencies into a self-contained, autonomous jar with an embedded HTTP listener.
- Select Java Version as 1.8. Java 1.8 is recommended for Spring 5 applications. Change other Maven properties such as Group, Artifact, and Package as shown in the following screenshot:

- Once completed, click on Next.
- The wizard will show the library options. In this case, since we are developing REST services, select Web under Web. This is an interesting step, which tells Spring Boot that a Spring MVC web application is being developed so that spring boot can include the necessary libraries, including Tomcat, as the HTTP listener and other configurations as required:

- Click on Finish.
- This will generate a project named chapter3.bootrest in STS Project Explorer:

- Let us examine the pom file. The parent element is one of the interesting aspects in pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M1</version>
</parent>
Spring-boot-starter-parent is a bill-of-materials (BOM), a pattern used by Maven's dependency management. The BOM is a special kind of pom used for managing different library versions required for a project. The advantage of using the spring-boot-starter-parent pom is that developers need not worry about finding the right, compatible versions of different libraries such as Spring, Jersey, Junit, Logback, Hibernate, Jackson, and more.
The starter pom has a list of Boot dependencies, sensible resource filtering, and sensible plug-in configurations required for the Maven builds.
The starter pom itself does not add jar dependencies to the project. Instead, it only adds library versions. Subsequently, when dependencies are added to pom.xml, they refer to the library versions from this pom.xml. Some of the properties are as shown next:
<activemq.version>5.14.5</activemq.version>
<commons-collections.version>3.2.2
</commons-collections.version>
<hibernate.version>5.2.10.Final</hibernate.version>
<jackson.version>2.9.0.pr3</jackson.version>
<mssql-jdbc.version>6.1.0.jre8</mssql-jdbc.version>
<spring.version>5.0.0.RC1</spring.version>
<spring-amqp.version>2.0.0.M4</spring-amqp.version>
<spring-security.version>5.0.0.M1</spring-security.version>
<thymeleaf.version>3.0.6.RELEASE</thymeleaf.version>
<tomcat.version>8.5.15</tomcat.version>
Reviewing the dependency section of our pom.xml, one can see that this is a clean and neat pom file with only two dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Since Web is selected, spring-boot-starter-web adds all dependencies required for a Spring MVC project. It also includes dependencies to Tomcat as an embedded HTTP listener. This provides an effective way to get all the dependencies required as a single bundle. Individual dependencies could be replaced with other libraries, such as replacing Tomcat with Jetty.
Similar to web, Spring Boot also comes up with a number of spring-boot-starter-* libraries, such as amqp, aop, batch, data-jpa, thymeleaf, and so on.
The last thing to be reviewed in the file pom.xml is the Java 8 property as shown here:
<java.version>1.8</java.version>
By default, the parent pom adds Java 6. It is recommended to override the Java version to 8 for Spring 5.
- Let us now examine Application.java. Spring Boot, by default, generated a class org.rvslab.chapter3.Application.java under src/main/java for bootstrapping:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
There is only a main method in the application, which will be invoked at startup, as per the Java convention. The main method bootstraps the Spring Boot application by calling the run method on SpringApplication. Application.class is passed as a parameter to tell Spring Boot that this is the primary component.
More importantly, the magic is done by the @SpringBootApplication annotation. @SpringBootApplication is a top-level annotation, which encapsulates three other annotations, as shown in the following code snippet:
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
The @Configuration annotation hints that the contained class declares one or more @Bean definitions. @Configuration is meta-annotated with @Component, therefore, they are candidates for component scanning.
@EnableAutoConfiguration tells Spring Boot to automatically configure the Spring application based on the dependencies available in the class path.
- Let us examine application.properties--a default application.properties file is placed under src/main/resources. It is an important file for configuring any required properties for the Spring Boot application. At the moment, this file is kept empty, and will be revisited with some test cases later in this chapter.
- Let us examine ApplicationTests.java under src/test/java. This is a placeholder for writing test cases against the Spring Boot application.
- As the next step, add a REST endpoint. Let us edit Application.java under src/main/java, and add a RESTful service implementation. The RESTful service is exactly the same as what was done in the previous project. Append the following code at the end of the Application.java file:
@RestController
class GreetingController{
@GetMapping("/")
Greet greet(){
return new Greet("Hello World!");
}
}
class Greet{
private String message;
public Greet() {}
public Greet(String message){
this.message = message;
}
//add getter and setter
}
- To run, go to Run As | Spring Boot App. Tomcat will be started on the 8080 port:

- We can notice the following things from the log:
- Spring Boot gets its own process ID (in this case, 3909)
- Spring Boot automatically starts the Tomcat server at the local host, port 8080
- Next, open a browser and point to http://localhost:8080. This will show the JSON response as follows:

The key difference between the legacy service and this one is that the Spring Boot service is self-contained. To make this clearer, run the Spring Boot application outside STS.
Open a terminal window, go to the project folder, and run Maven as follows:
$ maven install
This preceding command will generate a fat jar under the target folder of the project. Running the application from the command line shows the following:
$java -jar target/bootrest-0.0.1-SNAPSHOT.jar
As one can see, bootrest-0.0.1-SNAPSHOT.jar is self-contained, and could be run as a standalone application. At this point, the jar is as thin as 14 MB. Even though the application is not more than just a hello world, the spring boot service just developed practically follows the principles of microservices.
- Rust實戰(zhàn)
- Mastering matplotlib
- Building Mapping Applications with QGIS
- Unity Shader入門精要
- 差分進化算法及其高維多目標優(yōu)化應用
- Hands-On GPU:Accelerated Computer Vision with OpenCV and CUDA
- 微信小程序項目開發(fā)實戰(zhàn)
- Highcharts Cookbook
- Visual C#.NET程序設計
- Windows內核編程
- Instant Lucene.NET
- Mastering Unity 2D Game Development(Second Edition)
- Android應用開發(fā)實戰(zhàn)(第2版)
- 深度學習程序設計實戰(zhàn)
- 你好!Java