// Spring Boot 示例代码
// 1. 引入依赖 (pom.xml)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
// 2. 创建主应用程序类 (Application.java)
package com.example.demo;
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);
}
}
// 3. 创建一个简单的 REST 控制器 (HelloController.java)
package com.example.demo.controller;
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!";
}
}
pom.xml 文件中,我们添加了 spring-boot-starter-web 依赖,它包含了构建 Web 应用程序所需的所有依赖项。Application.java 是应用程序的入口点。使用 @SpringBootApplication 注解来启动 Spring Boot 应用程序,并调用 SpringApplication.run() 方法来运行应用程序。HelloController.java 是一个简单的控制器类,使用 @RestController 注解来定义一个 RESTful Web 服务。@GetMapping("/hello") 表示当访问 /hello 路径时,返回字符串 "Hello, Spring Boot!"。如果你需要更多功能或更复杂的例子,请告诉我!
上一篇:java获取文件大小
下一篇:java变量
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站