软件工程

第一个代码`package com.example.demo;

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 {

@GetMapping("/hello")
public String sayHello() {
    return "Hello, Spring Boot!";
}

}`
代码解析:

@RestController:组合注解,包含@Controller和@ResponseBody,表示这是一个控制器且返回值直接写入HTTP响应体
@RequestMapping:映射Web请求到指定路径
@GetMapping:GET请求的快捷映射注解
打开浏览器访问:
http://localhost:8080/api/hello

posted @ 2025-03-21 08:57  ytr123  阅读(8)  评论(0)    收藏  举报