【注解】@RestController、@GetMapping、@PostMapping、@RequestMapping等RESTful风格的Web服务的注解

一、映射 HTTP 请求的处理方法

1、@RestController:

@RestController 是 Spring 4.0 引入的一个注解,它是 @Controller@ResponseBody 的组合。
用于标识一个类,表示这个类是一个控制器,并且其中的方法会返回 JSON 格式的数据。通常用于构建 RESTful API。

@RestController
public class MyController {
    // Controller methods
}

2、@RequestMapping:

@RequestMapping 用于映射 HTTP 请求到控制器的处理方法。
可以用在类级别和方法级别。在类级别上设置的路径会与方法级别的路径拼接起来。
可以指定请求的方法(GETPOST等)、路径、参数等。

@Controller
@RequestMapping("/example")
public class MyController {
    @RequestMapping("/path")
    public String myMethod() {
        // Method implementation
        return "viewName";
    }
}
  • 指定单个请求方法:

    @RequestMapping(value = "/example", method = RequestMethod.GET) 
    public String handleGetRequest() {
        // 处理GET请求
        return "GET response"; 
    } 
    

    上面的例子中,handleGetRequest 方法只处理HTTP GET请求。

  • 指定多个请求方法:

    @RequestMapping(value = "/example", method = {RequestMethod.GET, RequestMethod.POST}) 
      public String handleGetOrPostRequest() {
          // 处理GET或POST请求
          return "GET or POST response"; 
      } 
    

    在 method 属性中使用数组,可以指定多个请求方法。

  • 传参
    @RequestMapping 注解通常不直接支持在运行时动态地传递参数。 @RequestMapping
    注解是一个元注解,它用于组合其他注解,而这些其他注解(例如
    @RequestParam@PathVariable)才提供了在运行时动态获取请求参数的功能。例如

    @RequestMapping(value = "/example", method = RequestMethod.GET) 
    public String handleRequest(@RequestParam String paramName) {
        return "Response with parameter: " + paramName; 
    }
    

3、@GetMapping:

@GetMapping@RequestMapping(method = RequestMethod.GET) 的缩写。用于处理 HTTP GET 请求的方法级注解。

@Controller
@RequestMapping("/example")
public class MyController {
    @GetMapping("/path")
    public String myMethod() {
        // Method implementation
        return "viewName";
    }
}

4、@PostMapping:

@PostMapping@RequestMapping(method = RequestMethod.POST) 的缩写。用于处理 HTTP POST 请求的方法级注解。

@Controller
@RequestMapping("/example")
public class MyController {
    @PostMapping("/path")
    public String myMethod() {
        // Method implementation
        return "viewName";
    }
}

这些注解一起使用,可以创建具有清晰结构的RESTful风格的控制器。@RequestMapping 提供了灵活的选项,而 @GetMapping@PostMapping 则是对特定 HTTP 方法的简化表示。@RestController 则简化了 @Controller@ResponseBody 的组合,使代码更加简洁。

二、处理了参数的注解

下面是一个简单的例子,演示了如何结合使用 @RequestMapping、@PathVariable@RequestParam

@RestController
@RequestMapping("/example")
public class ExampleController {

    @GetMapping("/path/{id}")
    public String handlePathWithPathVariable(@PathVariable Long id) {
        return "Response from path with id: " + id;
    }

    @GetMapping("/query")
    public String handleQueryParameter(@RequestParam String param) {
        return "Response from query parameter: " + param;
    }

    @PostMapping("/body")
    public String handleRequestBody(@RequestBody YourRequestBodyClass requestBody) {
        return "Response from request body: " + requestBody.toString();
    }
}

在这个例子中:

@GetMapping("/path/{id}") 使用了@PathVariable 来获取路径中的变量 id 的值。
@GetMapping("/query") 使用了 @RequestParam 来获取请求中的查询参数。
@PostMapping("/body") 使用了 @RequestBody 来获取请求体中的数据。
这种结合使用的方式使得你可以方便地从不同的位置获取请求参数,根据请求的不同部分,如路径、查询参数或请求体来进行处理。

@RequestParam@PathVariable、和 @RequestBody 是 Spring MVC 中用于获取请求参数的三个常用注解,它们分别用于不同的场景:

1、@RequestParam:

用于从请求的查询参数中获取值。通常用于处理表单提交或URL中的查询参数。

@RequestMapping("/example")
public String handleRequest(@RequestParam String paramName) {
    return "Response with parameter: " + paramName;
}

请求路径:/example?paramName=value

2、@PathVariable:

用于从请求路径中获取值。通常用于处理 RESTful 风格的URL,其中某些值是路径变量。

@RequestMapping("/example/{id}")
public String handleRequest(@PathVariable Long id) {
    return "Response with id: " + id;
}

请求路径:/example/123

3、@RequestBody:

用于从请求体中获取数据。通常用于处理 POST 或 PUT 请求中的 JSON 数据。

@RequestMapping(value = "/example", method = RequestMethod.POST, consumes = "application/json")
public String handleRequest(@RequestBody YourRequestBodyClass requestBody) {
    return "Response with request body: " + requestBody.toString();
}

在这里,YourRequestBodyClass 应该是一个与请求体JSON格式匹配的Java类。Spring会自动进行反序列化。

4、总结一下:

@RequestParam 用于获取请求的查询参数。
@PathVariable 用于获取请求路径中的变量。
@RequestBody 用于获取请求体中的数据。
这三个注解通常根据请求的类型和参数位置来选择使用。根据具体的业务需求,你可以选择其中的一个或多个来获取请求参数。

你可能感兴趣的:(java开发,JAVA杂项,restful,前端,python,后端,传参)