Spring Boot 2.0.1 Getting Started tutorial

Source: Internet
Author: User

Brief introduction

Spring boot is a set of basic configuration environments provided by spring that can be used to quickly develop products at the production environment level. Especially suitable for the development of micro-service architecture, save a lot of configuration trouble. For example, when you use spring MVC, you simply spring-boot-starter-web add dependencies to maven dependencies. In addition, it has the following features:

    • Create a separate spring project
    • Built-in Tomcat, Jetty,undertow
    • Initial POM configuration file to simplify MAVEN configuration
    • Automatically configure spring as much as possible
    • Provides production environment functions such as statistics, health checks and external configuration
    • No need for XML configuration and code generation
Create a Spring Boot app
    • Development environment: IntelliJ, JDK 1.8
    • Project Source Code Gitee

First create a MAVEN project in IntelliJ:

    • GroupID:cn.zxuqian
    • Artifactid:helloworld

After creation, the IntelliJ will prompt you to import the Maven configuration automatically, and select Enable Auto-import to start the automatic import. Then add the following code to the Pom.xml:

<?xmlVersion= "1.0" encoding= "UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>Cn.zxuqian</groupId>    <artifactId>HelloWorld</artifactId>    <version>1.0-snapshot</version>    <parent>        <groupId>Org.springframework.boot</groupId>        <artifactId>Spring-boot-starter-parent</artifactId>        <version>2.0.1.RELEASE</version>    </parent>    <dependencies>        <dependency>            <groupId>Org.springframework.boot</groupId>            <artifactId>Spring-boot-starter-web</artifactId>        </dependency>    </dependencies>    <properties>        <java.version>1.8</java.version>    </properties>    <build>        <plugins>            <plugin>                <groupId>Org.springframework.boot</groupId>                <artifactId>Spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

<dependencies>The tag adds spring-boot-starter-web dependencies, namely Spring MVC and the associated runtime environment. The spring-boot-maven-plugin plugin provides a set of Maven run targets that can be easily packaged, deployed, and run. Wait a moment. maven automatically downloads dependencies and then can write on the handwritten code.

Create the first controller

src/mainunder Create a new package cn.zxuqian.controllers and create a new class in it, name HelloController and add the following code:

package cn.zxuqian.controllers;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublicclass HelloController {    @RequestMapping("/")    publicindex() {        return"Hello World!";    }    }
    • @RestControllerMark this class as a rest controller and be ready to handle rest requests.
    • @RequestMapping("/")That is, this method handles a root-path request, such as http://localhost:8080/ .
    • indexThe method returns the string data, which is "Hello world".
Creating the Application Class

cn.zxuqiancreate the class under the package Application and add the following code:

package cn.zxuqian;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublicclass Application {    publicstaticvoidmain(String[] args) {        SpringApplication.run(Application.class, args);    }}
    • @SpringBootApplicationIndicates the startup class for this type of Spring boot app.
Run the App

Select the Right tab in IntelliJ Maven Projects , and then expand plugins-->spring-bootto select the spring-boot:run target. After the successful launch, the access seen in the browser http://localhost:8080 Hello World! is successful.

Article from my blog: http://zxuqian.cn/spring-boot-get-started/, welcome to visit.

Spring Boot 2.0.1 Getting Started tutorial

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.