Spring学习笔记_38——@RequestParam

@RequestParam注解

1. 介绍

RequestParam 是一个在 Java Web 开发中常用的注解,它属于 Spring 框架的一部分,用于处理 HTTP 请求中的参数。

RequestParams 注解用于将 HTTP 请求参数绑定到控制器处理方法的参数上。当一个控制器方法需要接收来自 HTTP 请求的参数时,可以使用此注解来指定参数的名称和其它属性

2. 场景

@RequestParam注解主要用于获取请求的参数值,斌关切为控制器类中的方法形参的参数赋值。

如果请求参数的名称与控制器中方法的形参的参数名称一致,@RequestParam注解可以省略。

如果使用@RequestParam注解没有获取到参数值时,可以使用@RequestParam注解提供默认的参数值。

  • 简单查询参数:当需要从GET去请求获取单个参数或多个参数时。
  • 可选参数:当请求参数不是必需的,可以为注解指定一个默认值
  • 数据转化:Spring可以自动将请求参数转换为相应的数据类型
  • 数据和集合:可以绑定请求参数到数组或集合类型的参数

3. 源码

java 复制代码
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.ui.Model;

/**
 * Annotation to bind individual request parameters to method parameters.
 * Typically used with {@link RequestMapping} or {@link GetMapping} / {@link PostMapping}.
 *
 * <p>For example, a GET request to "/users?name=John&age=25" could be handled as follows:
 *
 * <pre class="code">
 * &#64;GetMapping("/users")
 * public String handleRequest(&#64;RequestParam("name") String name,
 *                             &#64;RequestParam("age") int age,
 *                             Model model) {
 *     model.addAttribute("name", name);
 *     model.addAttribute("age", age);
 *     return "userView";
 * }
 * </pre>
 *
 * <p>This annotation may be used as a method parameter, to bind to a single request parameter,
 * or as a method return value, to bind to all request parameters in a {@link Model}.
 *
 * @author Arjen Poutsma
 * @since 3.0
 */
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
    /**
     * The name of the request parameter to bind to.
     */
    // String 类型的属性,指定URL中参数的名称
    String value() default "";
    
    /**
     * @since 4.2
     */
    @AlisaFor("value")
    // Spring4.2版本 开始提供的String类型的属性,与name属性作用相同。
    String name() default "";

    /**
     * Whether the request parameter is required.
     */
    // boolean类型的属性,指定对应的参数是否必须有值;
    // true, 必须有值
    // false: 可以没有值
    // 如果为true时,参数没有值就会报错
    boolean required() default true;

    /**
     * The default value to use if the request parameter is not provided.
     */
    // String类型的属性,指定参数没有值时的默认值。
    String defaultValue() default ValueConstants.DEFAULT_NONE;
}

4. Demo

java 复制代码
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class ProductController {

    @GetMapping("/products")
    public List<Product> searchProducts(
            @RequestParam(value = "category", required = false, defaultValue = "all") String category,
            @RequestParam(value = "minPrice", required = false, defaultValue = "0") Double minPrice,
            @RequestParam(value = "maxPrice", required = false, defaultValue = "1000000") Double maxPrice) {
        
        // 调用服务层方法,根据参数搜索产品
        return productService.search(category, minPrice, maxPrice);
    }
}
java 复制代码
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class CourseController {

    @GetMapping("/search-courses")
    public List<Course> searchCourses(
            @RequestParam(value = "courseName", required = false) String courseName,
            @RequestParam(value = "teacherName", required = false) String teacherName,
            @RequestParam(value = "difficulty", required = false, defaultValue = "all") String difficulty) {
        
        // 调用服务层方法,根据参数搜索课程
        return courseService.searchCourses(courseName, teacherName, difficulty);
    }
}
相关推荐
小斌的Debug日记4 分钟前
SpringBoot和微服务学习记录Day3
spring boot·学习·微服务
pwzs14 分钟前
Spring MVC 执行流程全解析:从请求到响应的七步走
java·后端·spring·spring mvc
路有瑶台19 分钟前
EXCEL学习
学习·excel
计算机视觉与OpenCV26 分钟前
自动驾驶与机器人算法学习
学习·机器人·自动驾驶
筱小虾米31 分钟前
Dify忘记管理员密码,重置的问题
人工智能·学习·dify
FAREWELL0007534 分钟前
C#进阶学习(九)委托的介绍
开发语言·学习·c#·委托
LCY1331 小时前
spring security +kotlin 实现oauth2.0 认证
java·spring·kotlin
我的代码永没有bug2 小时前
day1-小白学习JAVA---JDK安装和环境变量配置(mac版)
java·学习·macos
王有品2 小时前
Spring MVC 一个简单的多文件上传
java·spring·mvc
Zenexus2 小时前
Linux学习笔记协议篇(六):SPI FLASH设备驱动
linux·笔记·arm