preface

Before introducing SpringBoot, let’s first introduce the Spring framework. In the old days, people used to develop enterprise applications with J2EE. The development and deployment of heavy and complex EJBs can be frustrating for programmers. At that time, Spring framework came out of the blue. Through core dependency injection, AOP, transaction management and other core functions, it almost unified Java enterprise development application framework. After more than ten years of development, Spring has gradually changed from a swift young girl to a fat middle-aged woman. As more and more features become available and powerful, the workload of Spring’s configuration increases. Long configuration files often kill programmers. Spring is starting to annoy new programmers again. This time, it was Spring Boot that brought programmers back to Spring.

Spring Boot is not entirely new, it is based on Spring. SpringBoot is designed to make it easy for programmers to develop Spring applications. Spring Boot simplifies Spring-based development with a convention programming pattern. Minimize program configuration effort. The various XML configuration files of traditional Java Web development are almost invisible during Spring Boot development. Some people say that Spring’s annotation-based configuration can be done without XML files, but Spring Boot doesn’t even have a web.xml file, which shows you who’s tougher. And the programs developed by Spring Boot are no longer in the form of a WAR package and run in a Web container like Tomcat, but in a separate JAR package that can be run directly in the form of java-jar xxxxx.jar. Of course, Spring Boot also supports the way war packages run.

In addition to the advantages of Spring itself, Spring Boot provides the following features:

  1. He can create spring-based applications that run independently. Because Spring Boot has Tomcat, Jetty, and Undertow application servers embedded in it, it does not need to deploy a WAR file to run on a separate application server.
  2. It simplifies the configuration of project dependencies. Spring Boot categorizes and encapsulates our capabilities in the Java Web development process, simplifying Maven configuration by providing some optional “starter” POMs.
  3. Automate Spring configuration as much as possible. Spring Boot replaces traditional XML configuration with a list of programming conventions and annotations. Reduce configuration effort during Spring development. Make the configuration of development easier.
  4. Product-level application monitoring. By simply introducing corresponding dependencies, Spring Boot automatically adds production-level monitoring and management capabilities to the product. This includes health check and performance measurement.

In short, Spring Boot makes it easier to develop, deploy, and monitor Spring applications.

Let’s use a simple Hello Word to quickly understand the power of Spring Boot.

Create a project

Create a new project in Intellij and select Spring Initializr in the dialog box

Then select “Next” to fill in the basic project information

Continue to select “Next” to select the Spring Boot supported feature components to be included in the project. If you don’t know which components you want, you can skip this step and add them in maven’s POM file later.

Then, keep selecting “Next” until “Finish” is selected to complete the creation of the project. If you are creating a Spring Boot project for the first time, you need to wait for some time to complete the process of downloading dependent packages from the Internet.

After completion, the initial engineering structure of the Srping Boot is as follows:

Spring Boot 2.0 was released just a day or two ago, so the default Spring Boot version is now 2.0.0.REALEASE as shown in the red box.

Since we did not select any other functional components during the creation process, there is only one core dependency, spring-boot-starter.

SpringbootLearnApplication is the default system startup, the application, the properties file is the default configuration system configuration file.

Including SpringbootLearnApplication class is automatically generated, a SpringBootApplication annotations declared a SpringBoot applications. Class has only one line of code SpringApplication. Run (SpringbootLearnApplication. Class, args) to start the SpringBoot program.

package com.yanggaochao.springboot.springbootlearn; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootLearnApplication { public static void main(String[] args) { SpringApplication.run(SpringbootLearnApplication.class, args); }}Copy the code

Create an Http Rest service

In our work, Spring Boot is usually used to complete an Http Rest service. This is a Web-related function that requires the following dependencies in the POM file

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Copy the code

Configuration SpringBoot

An Http service requires configuring the port and context path on which the SpringBoot program runs the service. By default, this information is configured in the application.properties file by way of properties. As shown below.

server.port=3030
server.servlet.context-path=/learn
Copy the code

The SpringBoot service address is set to 3030 and the service context path is set to /learn.

Note: the server.servlet.context-path configuration corresponds to server.context-path in springBoot1. x

Write the Http Rest interface Hello

  1. A new kind of com. Yanggaochao. Springboot. Springbootlearn. Web. HelloApi to provide services.
  2. Add a RestController annotation to the class declaration declaring that the class is an Http Rest service.
  3. Add a RequestMapping(“/ API /v1/hello”) annotation to the class declaration to declare the address prefix for this Rest service.
  4. Write a sayHello method to provide the sayHello service. The PathVariable annotation before the parameter name declares that the parameter is fetched from the url path of the service.
  5. Add a RequestMapping(“/say/{name}”) to sayHello to declare the service address of the method. Actual address is HTTP | (HTTPS) : / / IP: 3030 / learn/API/v1 / hello/say / {name}. You should also specify the method to request the service, if not the default GET method.
package com.yanggaochao.springboot.springbootlearn.web; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * example Http Rest service ** @author * @since 2018-03-06 */ @restController @requestMapping ("/api/v1/hello")
public class HelloApi {
    @RequestMapping("/say/{name}")
    String sayHello(@PathVariable String name) {
        return "Hello," + name + "!"; }}Copy the code

Start the SpringBoot application

Is through the main method of SpringbootLearnApplication SpringBoot application startup. In Intellij IDEA, right-click on the body of the main method and select Run or Debug as normal Java applications do.

After SpringBoot is started, the banner information of the SpringBoot application is displayed and the startup log is displayed.

validation

Http Rest requests for the GET method can be made directly in the browser address bar. We in Chrome address bar enter http://localhost:3030/learn/api/v1/hello/say/yanggch, will be displayed in a browser Hello, yanggch!

packaging

Maven projects can build deployment packages using Maven’s package command. Click the Maven Project TAB on the right of IDEA to open the following interface, and click the content in the red box to complete the generation of the deployment package. The generated directory is in the target directory under the project directory. The SpringBoot deployment package defaults to a separate elementary-0.0.1- snapshot.jar file. Include all relevant files in this package.

Command line operation

The previous procedure is run in the IDEA tool. In actual deployment, the SpringBoot program is run from the command line.

Go to the directory where the SpringBoot deployment resides and run the Java -jar elementary 0.0.1 -snapshot. jar command to start the SpringBoot application.

Afterword.

So, with just a few lines of code and two configuration items, we implemented a basic Http Rest service interface. Isn’t it very fast and easy? Compare the steps of developing a Spring Http Rest service using traditional methods with the steps of developing Srping Boot to see the convenience of SpringBoot development. SpringBoot can also run independently. Even better, SpringBoot works well with the popular microservices framework Spring Cloud. SpringBoot is almost tailor-made for microservices.

Originally published in the Book of Jane,The original link