Add a Web function initiator

After adding Spring Boot basic dependencies, you only need to add the following initiator to use the Web MVC function. Spring Boot will automatically assemble the Web function.

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>  </dependency> </dependencies>Copy the code

Adding a startup class

It is recommended to put the startup class in the root package. It is also ok to put the Controller and startup class together.

To start a class, you must have a main method and add a startup method.

package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@SpringBootApplication
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}
Copy the code

Add a compile package plug-in

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>  <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> <fork>true</fork> <excludeDevtools>true</excludeDevtools> </configuration> </execution> </executions> </plugin> </plugins> </build>Copy the code

Three ways to run Spring Boot

  1. Run the main method of the startup class.
  2. Run the spring-boot:run command.
  3. After creating the jar package, run the Java -jar xx.jar command.

The default Spring Boot port is 8080, which can be changed with server.port=8081, or specified on the command line.

Localhost :8080/ : Hello World!

Spring Boot fast build method

You can also open Spring Boot’s quick build website, start.spring. IO /, to select the corresponding initiator generation project, and then import it.

Recommended reading

Dry goods: Free 2TB architect four-stage video tutorial

Interview: the most complete Java multithreaded interview questions and answers

Tools: Recommended an online creation flow chart, mind mapping software

Share Java dry goods, high concurrency programming, hot technology tutorials, microservices and distributed technology, architecture design, blockchain technology, artificial intelligence, big data, Java interview questions, and cutting-edge hot news.