Preparation work:
1. Intellij IDEA (ULTIMATE version): Official website download address
2. JDK
1. Create a new project
2. Select Spring Initializr on the left panel
Enter the project name and project group Name and project ID, click to proceed to the next step
The following page is used to add dependencies. You can add dependencies according to your needs. Or you can add it in the pom.xml file. Mainly include: Core (core dependency), SQL, NOSQL
For the current test, you only need to check Web.
Click Next to complete the project creation. The project structure is as follows: (Note: Example.java was added by me)
2. Create Example.java in the corresponding directory. The code is as follows:
package com.example.demo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } @RequestMapping("/hello/{myName}") String index(@PathVariable String myName) { return "Hello "+myName+"!!!"; } }
3. Run the project, select SpringbootTestApplication.java, right-click--Run 'SpringbootTestApplication', or click the button as shown:
4. The program starts successfully, and the console looks as follows (part):
5. Finally, let’s test it: enter http://localhost:8080/ and http://localhost:8080/hello/王达达
Test success! ! !
Finally, the pom.xml code is also attached for reference only.
<?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 "> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>springboot_test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot_test</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--这个就是我们刚刚勾选依赖时选择的 Web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
This is the first time I have come into contact with the SpringBoot architecture. Well, write it down first so as not to forget it later
The above is the detailed content of Chapter 1 of Getting Started with SpringBoot: Hello World. For more information, please follow other related articles on the PHP Chinese website!