首先,我们创建第一个SpringBoot项目,打开Idea,新建一个项目,选择Spring Initializer
注意修改成java 8版本
选择springboot的版本,此处选择2.6.2
选择web依赖
创建各层包
在Controller中写如HelloWorldController控制类
package com.example.springboot1study01.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
//interface:http://localhost:8080/hello
@RequestMapping("/hello")
public String hello(){
//call business
return "hello,world";
}
}
运行程序,在本机地址输入本机地址
http://localhost:8080/hello
运行结果如下,显示hello,world,则表示运行成功
pom.xml文件如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--GAV setting-->
<groupId>com.example</groupId>
<artifactId>SpringBoot-Study</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBoot-Study</name>
<description>SpringBoot-Study</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--web dependency: tomcat, dispatcher, xml-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--unit test-->
<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>