NOTES: Spring Boot project Build and parse

Source: Internet
Author: User
Tags spring initializr

Building a Maven Project

    • Use the official Spring INITIALIZR tool to generate the base project and access the http://start.spring.io/, as shown in this page, which provides the ability to build a Spring Boot project with maven.

    • Choose the Build tool Maven project,spring Boot version Select 1.5.4, fill in the Group and Artifact information, search for dependencies can be searched for other dependent packages, here we need to implement RESTful API, so you can add Web dependencies.
    • Click the Generate Project button to download the project's tarball and unzip the project package
    • Using the IDE to import the project, take Intellij idea 14 as an example, choose File->new->project from Existing Sources ... from the menu, select the Pom.xml file of the unzipped project folder, and click the OK button , click Next so we can create a basic spring Boot project

Engineering structure Analysis

As shown, Spring Boot has three blocks of infrastructure (specific paths vary according to the group and artifact that are filled when the user builds the project)

    • Src/main/java: Main program Entry springbootdemoapplication, you can start the Spring boot app by running the class directly
    • Src/main/resources: Configuration directory, which is used to store some configuration information of the application, such as application name, service port, database configuration, etc. Since we have applied the Web module, we have created the static directory and the templates directory, which is used to store the resources, slices, CSS, JavaScript and so on, which is used to store the template file of the Web page.
    • Src/test: Unit Test Catalog, generated springbootdemoapplicationtests implemented by JUNIT4, can be directly used to run the Spring boot application test.

MAVEN Configuration Analysis

To open the Pom.xml file under the current project, you can see the following key configurations:

    • Set the parent project of the current project, configured as follows

???????? <parent>

???????????????? <groupId>org.springframework.boot</groupId>

???????????????? <artifactId>spring-boot-starter-parent</artifactId>

???????????????? <version>1.5.4. RELEASE</version>

???????????????? <relativePath/><!--lookup parent from repository to

???????? </parent>

    • Set the code of the project source code and JDK version, configured as follows

???????? <properties>

???????????????? <project. build.sourceencoding>utf-8</project. build.sourceencoding >

???????????????? <project. reporting.outputencoding>utf-8</project. Reporting.outputencoding>

???????????????? <java. version>1.8</java. version>

???????? </properties>

    • Set project dependencies, the Spring-boot-starter-web project is a full-stack web development module with embedded TOMCAT, Spring Mvc;spring-boot-starter-test project as a generic test module that includes JUnit, Hamcrest and Mockito are configured as follows

???????? <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>

Implement RESTful API

in spring Boot in creating a restful API The implementation code is the same as Spring like MVC, you just don't need to do a lot of configuration with spring MVC, steps like this:

  • Create a new Hellocontroller class with the following code:

    @RestController

    public class Hellocontroller {

    ???????? @RequestMapping ("/hello")

    ???????? public String Index() {

    ???????????????? return"Hello World";

    ????????}

    }

  • start the app by Http://localhost:8080/hello , we can see that the expected result is returned: Hello World

Start Spring Boot Application

Start Spring Boot There are a number of ways to apply:

    • As a Java application, you can start by running the class that owns the main function directly.
    • When you deploy the runtime on a server, you typically first use the MVN Install package your app into jar package, then through Java -jar Xxx.jar to launch the app

Writing unit Tests

in spring Boot it is convenient to implement unit tests in src/test the unit test entry under Springbootdemoapplicationtests class, write a simple unit test to simulate an HTTP request, and test the code as follows:

Import Org.junit.Before;

Import org.junit.Test;

Import Org.junit.runner.RunWith;

Import org.springframework.boot.test.context.SpringBootTest;

Import Org.springframework.http.MediaType;

Import Org.springframework.test.context.junit4.SpringRunner;

Import ORG.SPRINGFRAMEWORK.TEST.WEB.SERVLET.MOCKMVC;

Import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

Import org.springframework.test.web.servlet.setup.MockMvcBuilders;

??

Import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;

Import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

??

@RunWith (Springrunner.class)

@SpringBootTest

public class Springbootdemoapplicationtests {

??

???????? private MOCKMVC mvc;

??

???????? @Before

???????? publicvoidsetUp() {

???????????????? MVC = Mockmvcbuilders.standalonesetup (new Hellocontroller()). Build();

????????}

??

???????? @Test

???????? publicvoidhellotest() throws Exception {

???????????????? Mvc.perform (Mockmvcrequestbuilders.get ("/hello"). Accept (Mediatype.application_json_utf8))

????????????????????????????????. Andexpect (Status (). IsOk ())

????????????????????????????????. Andexpect (Content (). String ("Hello World");

????????}

??

}

The code resolves as follows:

    • @RunWith (Springrunner. Class): Introduction of Spring support for JUNIT4
    • Mockmvc object: An interface initiation request used to impersonate the calling Controller, in the Hellotest test case method, the Perform function performs a request invocation, the accept is used to perform the received data type, Andexpect is used to determine the expected value returned by the interface
    • @Before: JUnit defines what is preloaded before a test case @Test content is executed, which is used to initialize the MOCKMVC instance

??

??

NOTES: Spring Boot project Build and parse

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.