温馨提示×

springboot怎么指定controller的url

小亿
95
2024-03-18 10:15:47
栏目: 编程语言

在Spring Boot中,可以使用@RequestMapping@GetMapping@PostMapping等注解来指定Controller的URL。以下是一些常用的例子:

  1. 使用@RequestMapping注解指定URL:
@RestController
@RequestMapping("/api")
public class MyController {

    @RequestMapping("/hello")
    public String hello() {
        return "Hello, world!";
    }
}
  1. 使用@GetMapping注解指定GET请求的URL:
@RestController
@RequestMapping("/api")
public class MyController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, world!";
    }
}
  1. 使用@PostMapping注解指定POST请求的URL:
@RestController
@RequestMapping("/api")
public class MyController {

    @PostMapping("/hello")
    public String hello() {
        return "Hello, world!";
    }
}

通过以上方式可以灵活地指定Controller的URL,并且可以支持不同类型的HTTP请求。

0