快速构建spring boot项目——入门Demo_springboot 入门项目demo
以下是一个简单的Spring Boot入门项目的代码示例。
首先,你需要在pom.xml
中添加Spring Boot的起步依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
然后,创建一个主应用类Application.java
:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
最后,创建一个简单的REST控制器HelloController.java
:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
}
以上代码构成了一个基本的Spring Boot项目,你可以通过运行Application
类中的main
方法来启动服务器,并通过访问http://localhost:8080/hello
来测试hello
端点。
评论已关闭