Simple use of the SpringBoot (web project)

Introduction to Spring Boot

When I first started learning JavaWeb, I used Servlet/JSP for development. It was a headache to create a Servlet for each interface. Later, I found ways like hidden fields or reflection to reduce the creation of Servlets, but it was still inconvenient. Then, I introduced frameworks like Struts2/SpringMVC to simplify our development. Compared to Servlet/JSP, productivity did improve significantly after introducing the framework. However, after using it for a while, I discovered new problems, such as cumbersome and error-prone configuration. When starting a new project, I had to set up the environment, and it always involved a few lines of configuration. The only difference between different projects might be the package, while most of the other configurations remained the same. Java has always been criticized for its cumbersome configuration and large code volume, and this is one of its manifestations. So what can I do? Spring Boot came into being. Spring Boot mainly provides the following functionalities:

  • It offers a convenient and quick entry experience for all Spring-based Java development.
  • It is ready to use out of the box. If you have custom configurations, use them; otherwise, use the default configurations provided by Spring Boot.
  • It provides a series of common non-functional features, such as embedded servers, security management, health checks, etc.
  • There is absolutely no code generation, and XML configuration is not required.

The emergence of Spring Boot has made Java development simple again because it truly solves the pain points in development. Therefore, this technology has been widely used. Many friends of mine who interview Java engineers have been asking about Spring Boot since early 2017. The popular Spring Cloud microservices are also based on Spring Boot. Therefore, it is necessary for all Java engineers to master Spring Boot.

Notice: Spring boot needs to use Maven.

After successfully creating a Spring Boot project, it can be overwhelming to see so many files. In essence, many Spring Boot project is just a Maven project.


If you are not familiar with Maven, I recommend using IntelliJ IDEA Community Edition, as it is straightforward.

To create a Maven project, click “Next” and fill in the basic information for the Maven project in the next step.

Then click “Next” to complete the project creation.

After the project is created, add the following dependencies to the pom.xml file:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Once the dependencies are added successfully, create a package under the java directory and create a startup class named App, as follows:

@EnableAutoConfiguration
@RestController
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
    
    @GetMapping("/springblog")
    public String springblog() {
        return "hello springblog";
    }
}

The @EnableAutoConfiguration annotation enables auto-configuration.

You can start a Spring Boot project by executing the main method. The pom.xml file contains Maven coordinates.

In fact, Spring Boot is quite simple.

Let me give you another simple example:

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
@RequestMapping("/springblog")
public class TestController {
    @RequestMapping("/hi")
    public String hi(){
        return "Hello springblog";
    }
}

try to running curl localhost:8080/springblog/hi and see the result.

good spring for you :D.

No Comments

Send Comment Edit Comment


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
:D
Emoji
picure
black!
Previous
Next