Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get started with Spring Boot 2.x

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Spring Boot 2.x how to get started, for this question, this article introduces in detail the corresponding analysis and solutions, hoping to help more partners who want to solve this problem to find a more simple and easy way.

What is Spring Boot?

Spring Boot is a new framework provided by the Pivotal team, which is designed to simplify the initial building and development process of new Spring applications. The framework follows the idea of "convention is better than configuration", removes those template configurations that originally used the Spring framework, inherits the excellent genes of the original Spring framework, and helps developers to develop applications quickly.

Characteristics of SpringBoot

Generally speaking, it is simple, fast and convenient.

The core module of SpringBoot

Create a SpringBoot project

This article uses the development tool eclipse

Maven construction project on the official website

1. Visit https://start.spring.io/ 2, select Maven Project, Java, Spring Boot version 2.1.8 in the build tool, and basic information of some projects, as shown in the following figure:

3. Click Generate Project to download the project package 4, Import-> Existing Maven Projects-> Next-> Select the decompressed folder-> Finsh

Eclipse build project

1. First install the SpringBoot plug-in, Help-> Eclipse Marketplace-> search 'Spring'-> install Spring Tools 4-for Spring Boot-> Install until restart 2 and File-> New-> Project are completed, pop up box 3 of the new project, search' Spring', find and select Spring Starter Project under the SpringBoot subdirectory, click Next 4, fill in the relevant project information, click Next, select the dependency package you need, and then click Next to confirm that the Finish is correct. Complete the creation.

HelloWorld

We build a helloworld project based on the above, based on which we implement a simple web example and a test sample

1. Introduction to the structure of project directory

As shown in the figure above, the infrastructure of Spring Boot consists of three large blocks: 1, the src/main/java Java source code directory, the main program entry HelloworldApplication, you can run this class directly to start the Spring Boot application 2, src/main/resources resource file directory, which is used to store some of the application's configuration and static resources. Application.properties is a configuration file that allows you to configure application names, server ports, database links, and so on. Due to the introduction of the Web module, there are static directory and templates directory, static is used to store static resources, such as pictures, CSS, JavaScript, etc., and templates is used to store template files for Web pages. 3. Src/test/java unit test directory, the generated HelloworldApplicationTests is realized by JUint 4, and can be directly used to run the test of Spring Boot application.

2.Maven configuration Analysis 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.8.RELEASE com.example helloworld 0.0.1-SNAPSHOT jar helloworld Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test Org.springframework.boot spring-boot-maven-plugin

Spring Boot version: 2.1.8.RELEASE

Packaging form: jar (default packaging form for Spring Boot)

The project dependency dependencies in the pom.xml file has two modules by default:

Spring-boot-starter-web full-stack Web development module, including embedded Tomcat, Spring MVC.

Spring-boot-starter-test general test module, including JUnit, Hamcrest, Mockito. The build part of the project build: the Maven plug-in for Spring Boot is introduced.

3. Implement a simple application

Create a new package, named com.example.demo.controller, and you can modify your own path according to the actual build situation.

Create a new HelloController class with the following code:

@ RestControllerpublic class HelloController {@ RequestMapping ("/ hello") public String hello () {return "Hello World";}}

Launch the application, visit http://localhost:8080/hello through the browser, and we can see that the expected result is returned: Hello World.

4. Unit testing

Open the test entry HelloApplicationTests under src/test/java and write a simple unit test to simulate the HTTP request. The code is as follows:

@ RunWith (SpringJUnit4ClassRunner.class) / / introduce Spring's support for JUnit4 @ SpringBootTestpublic class HelloApplicationTests {private MockMvc mvc;// is used to simulate the API calling Controller to initiate a request, @ Before / / preload the content to initialize the simulation public void setUp () throws Exception {mvc=MockMvcBuilders.standaloneSetup (new HelloController ()). Build () } @ Test public void hello () throws Exception {mvc.perform (MockMvcRequestBuilders.get ("/ hello") .accept (MediaType.APPLICATION_JSON)) .andexpect (status () .isOk ()) .andexpect (content () .string (equalTo ("Hello World") }}

Note that the following static references need to be introduced to make the status, content, and equalTo functions available:

Import static org.hamcrest.Matchers.equalTo; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

Debugging in the development environment introduces hot deployment dependencies, so there is no need to restart manually after modifying the code.

Org.springframework.boot spring-boot-devtools true can use Spring Boot to build projects very conveniently and quickly, so that we do not have to worry about the compatibility between frameworks, applicable versions and other issues, we want to use anything, just add a configuration, so using Spring Boot is very suitable for building micro services. This is the answer to the question about how to get started with Spring Boot 2.x. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report