// 示例代码:Java后端开发中的一个简单的RESTful API示例,使用Spring Boot框架
// 1. 创建一个Spring Boot应用程序的主类
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) {
SpringApplication.run(DemoApplication.class, args);
}
}
// 2. 创建一个控制器类来处理HTTP请求
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class HelloController {
// 定义一个GET请求的处理方法
@GetMapping("/hello")
public String sayHello() {
return "Hello, World!";
}
}
// 3. 配置文件 application.properties (位于 src/main/resources 目录下)
# server.port=8080
// 解释说明:
// - `DemoApplication` 类是Spring Boot应用程序的入口点,它使用了 `@SpringBootApplication` 注解来启用自动配置和组件扫描。
// - `HelloController` 类是一个REST控制器,它处理来自 `/api/hello` 的GET请求,并返回一个简单的字符串响应。
// - `application.properties` 文件用于配置应用程序的运行参数,例如服务器端口。
// 运行此应用程序后,可以通过访问 http://localhost:8080/api/hello 来获取 "Hello, World!" 响应。
上一篇:java生成uuid方法
下一篇:java runtime
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站