First experience with SpringBoot: the first Spring Boot program

mission details

Spring Boot is the starting point built on all Spring-based applications and aims to quickly start and run with minimal Spring front-end configuration.

This task: Create a web program using Spring Boot.

related information

In order to master this level of knowledge, you need to master:

如何创建一个Spring Boot程序;
如何创建一个web程序。

1. Create a Spring Boot program

Here is the simplest way. The official website provides a website to directly generate a Spring Boot project. The address is https://start.spring.io/. Enter this address and we will proceed as follows.

Insert picture description here

After downloading the compressed package, unzip it to get a demo folder. We then open our idea, file->open-> select the demo folder, and open the project.

Insert picture description hereInsert picture description here

Create a web application

The SpringBoot project has been created successfully. Let's create a web project based on this to realize the interaction between the front end and the back end.

1. First, we add the following dependencies to the pom.xml file of the imported project:

  <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

Note: This dependency introduces related jar packages needed for web module development, and the version information is controlled by spring-boot-starter-parent.

2. Then create a controller package, and create a HelloController class below:
Insert picture description here Note: When you enter @RestController for the first time, you need to import the configuration file, directly press ALT+Enter, and you are done.

@RestController, this means that Spring MVC can use it to handle web requests. The @RequestMapping annotation provides "routing" information. It tells Spring that any HTTP request with a /path should be mapped to the index() method.
When invoked from a browser or using curl on the command line, this method returns plain text. This is because @RestController combines @Controller and @ResponseBody, the two annotations cause the web request to return data instead of a view.

3. Run the web program

After running, the following error occurs.
Insert picture description hereWe can know that this is because our 8080 port is occupied and the operation failed. Here we can go to the task manager to close the process that uses the 8080 port, or reassign the port for the project. Re-allocate the port as follows:
Insert picture description here

After running successfully, open the browser and enter http://localhost:9090/hello
Insert picture description here

Programming requirements

Add dependency
Insert picture description here
completion code

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {
    
    

    //添加路由并编写相应方法
	/**********   Begin   **********/
	@RequestMapping("/hello")
	public String hello(){
    
    
	return "Say Hello!";
	}
 
   /**********   End   **********/

}

starting program

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
    
    

	public static void main(String[] args) {
    
    
		
		//启动Spring Boot
		/**********   Begin   **********/
       SpringApplication.run(DemoApplication.class,args);
		
		/**********   End   **********/

	}

}

to sum up:

This simple program only sends data from the backend to the frontend, writes the data content back to the frontend in the controller layer, and provides a route for the frontend to access the backend data.

Guess you like

Origin blog.csdn.net/Zheng_lan/article/details/105503348