本文共 7437 字,大约阅读时间需要 24 分钟。
作为一个经常从事 Spring 开发的开发者,我会基于实际项目经验,向大家详细介绍 Spring 中常用的 RESTful API 注解及其使用方法。
@Controller 是 Spring 的一个核心注解,用于标注处理 HTTP 请求的控制器类。它的作用是将 HTTP 请求映射到具体的方法上。
package org.springframework.stereotype;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic @interface Controller { String value() default "";} @RequestBody 是一个常用的注解,用于将 HTTP 请求的正文绑定到一个对象上。它主要用于处理 POST、PUT 等请求,适用于数据插入场景。
package org.springframework.web.bind.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({ElementType.TYPE, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface RequestBody { String value() default "";} @RestController 是 @Controller 和 @ResponseBody 的综合注解,用于标注处理 RESTful API 请求的控制器类。
package org.springframework.web.bind.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.stereotype.Controller;@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Controller@ResponseBodypublic @interface RestController { String value() default "";} @RequestMapping 是 Spring 用于配置 URL 映射的注解,既可以用在类上,也可以用在方法上。
package org.springframework.web.bind.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.core.annotation.AliasFor;@Target({ElementType.METHOD, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Mappingpublic @interface RequestMapping { String name() default ""; @AliasFor("path") String[] value() default {}; @AliasFor("value") String[] path() default {}; RequestMethod[] method() default {}; String[] params() default {}; String[] headers() default {}; String[] consumes() default {}; String[] produces() default {};} name():用于指定请求映射的名称。value():用于指定请求路径。method():用于指定请求方法(如 GET、POST 等)。produces():用于指定请求内容类型。@GetMapping 是 @RequestMapping method = RequestMethod.GET 的缩写,用于将 HTTP GET 请求映射到特定的处理方法上。
package org.springframework.web.bind.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.core.annotation.AliasFor;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documented@RequestMapping(method = {RequestMethod.GET})public @interface GetMapping { @AliasFor(annotation = RequestMapping.class) String name() default ""; @AliasFor(annotation = RequestMapping.class) String[] value() default {}; @AliasFor(annotation = RequestMapping.class) String[] path() default {}; @AliasFor(annotation = RequestMapping.class) String[] params() default {}; @AliasFor(annotation = RequestMapping.class) String[] headers() default {}; @AliasFor(annotation = RequestMapping.class) String[] consumes() default {}; @AliasFor(annotation = RequestMapping.class) String[] produces() default {};} @RequestBody 注解用于将 HTTP 请求的正文绑定到一个对象上,常用于处理 POST 请求中的数据插入。
package org.springframework.web.bind.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({ElementType.TYPE, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface RequestBody { String value() default "";} @RequestParam 是从 HTTP 请求的请求参数中获取值的注解,常用于 GET 请求中的参数获取。
package org.springframework.web.bind.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({ElementType.TYPE, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface RequestParam { String name() default ""; String value() default "";} @PathVariable 是从 URI 模板中获取值的注解,常用于 RESTful API 中的 URI 模板参数获取。
package org.springframework.web.bind.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({ElementType.TYPE, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface PathVariable { String name() default ""; String value() default "";} @RestController 是一个组合注解,它等同于 @Controller + @ResponseBody。两者的主要区别在于 @RestController 更简洁,通常用于 RESTful API 的控制器类。
@RestControllerpublic class MyRestController { @GetMapping("/api/test") public String test() { return "hello world"; }} 和
@Controller@ResponseBodypublic class MyController { @GetMapping("/api/test") public String test() { return "hello world"; }} 在功能上是等价的,建议在项目中统一使用 @RestController,以保持代码简洁。
在实际项目中,可以将多个注解组合使用,例如:
@RestController@RequestMapping("/api")public class UserController { @GetMapping("/users") @RequestBody List users; @GetMapping("/{id}") @RequestBody User user(@PathVariable Long id); @PostMapping @RequestBody User user(@RequestBody User userDetails) { // 处理用户数据 }} 通过合理组合这些注解,可以实现复杂的 RESTful API 请求处理。
通过以上内容,希望大家能够更好地掌握 Spring 中的 RESTful API 注解的使用方法,快速开发出高效的 Web 应用程序。
转载地址:http://qaqfk.baihongyu.com/