官术网_书友最值得收藏!

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.

The full source code of this example is available as the chapter3.Bootrest project in the code files of this book under the following Git repository: https://github.com/rajeshrv/Spring5Microservice
  1. 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:
  1. The Spring Starter Project is a basic template wizard, which provides a selection of a number of other starter libraries.
  2. 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.
  1. 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:
  1. Once completed, click on Next.
  1. 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:
  1. Click on Finish.
  1. This will generate a project named chapter3.bootrest in STS Project Explorer:
  1. 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.

Refer to the following link to see the different dependencies provided in the starter parent (version 2.0.0). All these dependencies can be overridden if required: https://github.com/spring-projects/spring-boot/blob/a9503abb94b203a717527b81a94dc9d3cb4b1afa/spring-boot-dependencies/pom.xml

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.

  1. 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.

  1. 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.
  1. Let us examine ApplicationTests.java under src/test/java. This is a placeholder for writing test cases against the Spring Boot application.
  1. 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
}
  1. To run, go to Run As | Spring Boot App. Tomcat will be started on the 8080 port:
  1. 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.

主站蜘蛛池模板: 武安市| 金门县| 郯城县| 武安市| 南京市| 无棣县| 丰顺县| 克拉玛依市| 遂溪县| 惠来县| 金沙县| 九寨沟县| 安康市| 广安市| 全州县| 灵璧县| 二连浩特市| 本溪市| 健康| 黄陵县| 吉林市| 玛沁县| 沅江市| 石台县| 江西省| 凌源市| 若尔盖县| 繁昌县| 湘西| 宁陵县| 娱乐| 新蔡县| 根河市| 阆中市| 东至县| 新兴县| 尼玛县| 固安县| 金溪县| 潜山县| 华容县|