Step by step learning SpringBoot (1) quickly build a web and springboot build a web

Source: Internet
Author: User

Step by step learning SpringBoot (1) quickly build a web and springboot build a web
Adapt to readers

  • Front-end engineers (java companies)
  • Front-end Architect (a java Company)
  • Java Engineer
  • Test Engineer (java Company)

 

What is SpringBoot?

Spring Boot is a project promoted by the community feedback. Spring Boot is one of the most influential projects in Spring and even the Java Community in the past five years. Spring Boot mainly includes the following features: directly embed Tomcat, Jetty, or Undertow as Servlet container. Since then, you no longer need to package the application into war and then upload it to the application server. The starter POM is provided to facilitate package management, greatly reducing jar hell or dependency hell's automatic Spring framework configuration, saving programmers a lot of time and energy, so that programmers can focus on writing business logic code without any third-party systems, and do not need to use xml for framework configuration, SpringBoot makes java applications simpler.

SpringBoot environment preparation
  • Jdk1.8 or later (go to the java official website to download. Remember to download jdk1.8 or later)
  • Maven3.0 + or gradle2.3 +. maven is used in this article.
  • An ide you like. The demo in this article uses idea.
Create a java project using idea

Open idea, create a project, and select Maven

Click next, set projectname to helloworld, and click finish as follows,

A simple java project has been created.

Build a SpringBoot jar package and dependency package through maven

Maven is an excellent java build tool. We use maven to build the jar package and dependencies of springboot. maven is easy to use. This article does not focus on the usage of maven, if you are not familiar with maven, you can go to the official website to learn.

Open pom. xml and copy the following content.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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>helloWorld</groupId>
<artifactId>helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.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>

<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>

</project>

After saving pom. xml, idea built through maven. Now this java project has the SpringBoot feature.

 

Write a controller


Here we use an annotation of the SpringBoot feature.

@ RestController

It is equivalent to the combination of SpringMVC @ Controller and @ ResponseBody. Don't worry. We will introduce it in detail in the following article. Let's first run the program. The Code is as follows.

package com.helloworld.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * Created by fangxiao on 2017/3/24. */@RestControllerpublic class Controller {    @RequestMapping("/helloworld")    public String helloWorld() {        return "helloworld";    }}

 

Write an Application to start the main method

 

SpringBoot directly starts the built-in tomcat using the main method. Don't worry. Let's run the program first. The program is as follows:

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * Created by fangxiao on 2017/3/25. */@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

Here is the most amazing annotation @ SpringBootApplication. How does it work? I will discuss it later.

 

Start the service

After coding, we need to start a web service. the startup method can be run by using the gradle or maven command. I am used to directly start it by using the jar command. For IEDA, run the Application class directly.

You can see that the built-in tomcat (the default application server built in spring boot is tomcat) has been started,

We just created a route/helloworld in the controller. Open the browser and enter localhost: 8080/helloworld. the following Response is displayed:

Since then, we have already set up a java web, and some colleagues may ask, if you want to response a json interface how to implement it, this is also very easy, add a route in the controller, the Code is as follows,

   @RequestMapping("/user")    public Map getUsers() {        Map map = new HashMap<>();        map.put("name","fangxiao");        map.put("age","15");        return map;    }

Start the server and access localhost: 8088/user. The result is as follows:

This is a simple interface service, and the front-end can use ajax to obtain the corresponding data rendering.

Here, you may have some questions. Just a few lines of code will start a web service without configuring the web. xml, you do not need to configure other spring dependencies for injection, and you do not need to maintain a tomcat or war package. These are the exciting parts of SpringBoot. The conventions are greater than the configurations, greatly simplifying the configuration cost, in the subsequent articles, I will gradually introduce more features and principles.

 

 

Summary

In this article, we quickly set up a web by using the SpringBoot feature. We started a web with only a few lines of code and accessed a Restful service, the idea that the conventions are better than the configuration method is more concise than SpringMVC. I hope this series of ideas can be provided to you and some attempts. All examples can be found at https://github.com/fangxiao/springboot.pdf. If you want to learn more technologies, please share your.

 

Related Article

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.