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 use Spring Boot

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to use Spring Boot. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Brief introduction

When you first came into contact with and learned the Spring framework, did you retreat because of its complex configuration? Do you find a bunch of repeatedly pasted configurations a little boring when you use the Spring framework for the nth time? Then you might as well try using Spring Boot to make it easier to use and build Spring applications more easily and quickly!

Spring Boot makes our Spring application lighter. For example, you can just rely on a Java class to run a Spring reference. You can also package your application as jar and run your Spring Web application by using java-jar.

The main advantages of Spring Boot are:

Get started faster for all Spring developers

Right out of the box, various default configurations are provided to simplify project configuration

Embedded containers simplify Web projects

No requirements for redundant code generation and XML configuration

Getting started

The main goal of this chapter is to complete the construction of the Spring Boot basic project, and to achieve a simple Http request processing, through this example to have a preliminary understanding of Spring Boot, and experience its simple structure, rapid development features.

System requirements:

Java 7 and above

Spring Framework 4.1.5 and above

This paper adopts Java 1.8.0 to 73 and Spring Boot 1.3.2 to debug.

Build a project using Maven

1. Generate the basic project through the SPRING INITIALIZR tool

Visit: http://start.spring.io/

Select the build tool Maven Project, Spring Boot version 1.3.2 and some basic engineering information, please refer to the SPRING INITIALIZR shown in the following figure

Click Generate Project to download the project package

2. Decompress the project package and import it into Maven project with IDE. Take IntelliJ IDEA 14 as an example:

Select File- > New- > Project from Existing Sources... from the menu

Select the extracted project folder and click OK

Click Import project from external model and select Maven, and click Next to the end.

If your environment has more than one version of JDK, please choose a version above Java 7 when choosing Java SDK.

Analysis of project structure

The basic project is created through the above steps. As shown in the figure above, the infrastructure of Spring Boot consists of three files (the specific path is based on all the differences of Group entered when the user generates the project):

Program entry under src/main/java: Chapter1Application

Configuration file under src/main/resources: application.properties

Test entry under src/test/: Chapter1ApplicationTests

Both the generated Chapter1Application and Chapter1ApplicationTests classes can be run directly to start the currently created project, and since the project does not currently work with any data access or Web modules, the program will finish running after loading the Spring.

Introduction of Web module

The current pom.xml content is as follows, with only two modules introduced:

Spring-boot-starter: core modules, including autoconfiguration support, logging, and YAML

Spring-boot-starter-test: test module, including JUnit, Hamcrest, Mockito

Org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test

To introduce Web module, you need to add spring-boot-starter-web module:

Org.springframework.boot spring-boot-starter-web

Write HelloWorld services

Create a package named com.didispace.web (modified according to the actual situation)

Create the HelloController class as follows

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

Start the main program, open a browser to access http://localhost:8080/hello, and you can see the page output Hello World

Write unit test cases

Open the test entry Chapter1ApplicationTests class under src/test/. Let's write a simple unit test to simulate the http request, as follows:

@ RunWith (SpringJUnit4ClassRunner.class) @ SpringApplicationConfiguration (classes = MockServletContext.class) @ WebAppConfigurationpublic class Chapter1ApplicationTests {private MockMvc mvc; @ Before public void setUp () throws Exception {mvc = MockMvcBuilders.standaloneSetup (new HelloController ()). Build ();} @ Test public void getHello () throws Exception {mvc.perform (MockMvcRequestBuilders.get ("/ hello") .accept (MediaType.APPLICATION_JSON)) .andexpect (status (). IsOk ()) .andExpect (content (). String (equalTo ("Hello World"));}

Use MockServletContext to build an empty WebApplicationContext so that the HelloController we create can be created in the @ Before function and passed to the MockMvcBuilders.standaloneSetup () function.

Note the introduction of the following 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

Now that the goal is achieved, a blank Spring Boot project is built through Maven, and a simple request processing is implemented by introducing the web module.

Thank you for reading! This is the end of the article on "how to use Spring Boot". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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

Development

Wechat

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

12
Report