Spring-boot Combat "01": Hello World Project Building

Source: Internet
Author: User
Tags spring initializr

Brief introduction

Did you retreat from the complex configuration when you first contacted and learned the spring framework for the 1th time? When you use the Spring framework for Nth times, do you feel bored with a bunch of sticky configurations? Try spring boot to make it easier and faster to build your spring app!

Spring boot makes our spring application even lighter and more lightweight. For example, you can simply rely on a Java class to run a spring reference. You can also package your app as a jar and run your spring Web app by using Java-jar.

Key Benefits of Spring boot:

    • Get started faster for all spring developers
    • Out-of-the-box with a variety of default configurations to simplify project configuration
    • Inline container simplifies web projects
    • No requirements for redundant code generation and XML configuration

Quick Start

The main objective of this chapter is to build the Spring Boot Foundation project and implement a simple HTTP request processing, which gives a preliminary understanding of spring boot and experiences its simple structure and rapid development.

System Requirements:
    • Java 7 and above
    • Spring Framework 4.1.5 and above

This article Java 1.8.0_144 adopts Spring Boot 1.5.6 , debugging passes.

Building projects with Maven
  1. SPRING initializr tool to produce the underlying project
    1. access: http://start.spring.io/
    2. Select Build Tool Maven Project , Spring boot version 1.5.6 and some engineering basic information, see spring initializr
    3. click Generate project Download the project compression package
  2. Unzip the project package and use the IDE to Maven import the Spring Tool Suite project as an example:
    1. menu, select File –> New –>Project from Existing Sources...
    2. Select the extracted Items folder and clickOK
    3. Click Import project from external model and select Maven , and click to the Next end.
    4. If you have multiple versions of the JDK in your environment, please select the Java SDK above version when you choose Java 7
Project Structure Analysis

Project structure

The following steps complete the creation of the base project, as shown in the Spring boot infrastructure with a total of three files (the exact path varies according to the group all the differences that the user fills in when the project is generated):

    • src/main/javaUnder the program entry:YucongDemoApplication
    • src/main/resourcesThe following configuration file:application.properties
    • src/test/Test entry under:YucongDemoApplicationTests

Both the generated YucongDemoApplicationTests and class can run directly to start the currently created project, and because the project is currently not mated to any data access or Web modules, the program ends up running after the spring has finished loading. YucongDemoApplication

Introducing the Web Module

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

    • spring-boot-starter: core modules, including automatic configuration support, logs, and Yaml
    • spring-boot-starter-test: Test modules, including JUnit, Hamcrest, Mockito
123456789101112 <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies>

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

1234 <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency>
Writing HelloWorld Services
    • Create package a named com.didispace.web (modified according to the actual situation)
    • Create HelloController a class with the following content
123456789 @RestController public class Hellocontroller { @RequestMapping ("/hello")public String Index() {return "Hello World";} }
    • Start the main program, open the browser access http://localhost:8080/hello , you can see the page outputHello World
Writing Unit Test Cases

Open the src/test/ test entry class under Chapter1ApplicationTests . The following is a simple unit test to simulate an HTTP request, as follows:

1234567891011121314151617181920 @RunWith (Springjunit4classrunner.class)@SpringApplicationConfiguration (classes = mockservletcontext.class)@WebAppConfigurationpublic class chapter1applicationtests { private MOCKMVC MVC;@Beforepublic void setUp() throws Exception { MVC = Mockmvcbuilders.standalonesetup (new Hellocontroller ()). build ();}@Testpublic 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 one WebApplicationContext so that we can create HelloController it @Before and pass it to the function in the function MockMvcBuilders.standaloneSetup() .

    • Note that the following content is introduced to make, status content and equalTo function available
123 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 has been completed, a blank spring boot project has been built through MAVEN, and a simple request processing is implemented by introducing a Web module.

Spring-boot Combat "01": Hello World Project Building

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.