Spring Boot中的@GetMapping注解,如何使用
warning:
这篇文章距离上次修改已过418天,其中的内容可能已经有所变动。
@GetMapping是一个组合注解,由@RequestMapping(method = RequestMethod.GET)派生而来。它用于映射HTTP GET请求到具体的处理方法上。在Spring Boot中,你可以使用@GetMapping来创建RESTful API,处理HTTP GET请求。
以下是一个简单的例子,演示如何在Spring Boot中使用@GetMapping:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@GetMapping("/greeting")
public String greeting() {
return "Hello, World!";
}
}在这个例子中,我们创建了一个名为MyController的控制器类,并在其中定义了一个方法greeting()。我们使用@GetMapping注解将HTTP GET请求映射到路径/greeting。当客户端发送一个GET请求到/greeting时,Spring Boot会调用greeting()方法,并返回字符串"Hello, World!"。
评论已关闭