Spring MVC注解中@PathVariable和@RequestParam的区别

@PathVariable注解的用法和作用

映射 URL 绑定的占位符

spring3.0的一个新功能:接收请求路径中占位符的值

通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过注解@PathVariable(“xxx“) 绑定到操作方法的入参中。

  1. @RequestMapping(value = "/index/{id}"
  2. 请求路径:http://localhost:8080/hello/user/index/1
    @RequestMapping(value = "/index/{id}", method = RequestMethod.GET)
    public String index(@PathVariable("id") int id) {
        System.out.println("get user:" + id);
        return "success";
    }

@RequestParam绑定请求参数到控制器方法参数

在控制器方法入参处使用@RequestParam注解可以将请求参数传递给方法,value属性指定参数名,required属性指定参数是否必需,默认为true,表示请求参数中必须包含对应的参数,如果不存在,则抛出异常。

请求路径:http://localhost:8080/hello/user/requestParam?loginName=kj&loginPwd=123

    @RequestMapping("/requestParam")
    public String requestParam(@RequestParam(value = "loginName")String loginName,@RequestParam(value = "loginPwd")String loginPwd) {
        System.out.println("loginName:"+loginName+",loginPwd:"+loginPwd);
        return "success";
    }

区别

1.用法上的不同:

从名字上可以看出来,PathVariable只能用于接收url路径上的参数,而RequestParam只能用于接收请求带的params 。

package com.lrm.springbootdemo.web;

import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1")
public class HelloController {

    @GetMapping("/books/{username}")
    public Object testPathVariable(@PathVariable String username){
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        return map;
    }

    @PostMapping("/books2")
    public Object testRequestParam(@RequestParam("name") String name,
                       @RequestParam("author") String author,
                       @RequestParam("isbn") String isbn) {
        Map<String, Object> book = new HashMap<String, Object>();
        book.put("name", name);
        book.put("author", author);
        book.put("isbn", isbn);
        return book;
    }

    @PostMapping("/books2/{id}")
    public Object test(@PathVariable("id") long id,@RequestParam("name") String name,
                       @RequestParam("author") String author,
                       @RequestParam("isbn") String isbn) {
        Map<String, Object> book = new HashMap<String, Object>();
        book.put("id",id);
        book.put("name", name);
        book.put("author", author);
        book.put("isbn", isbn);
        return book;
    }
}

testPathVariable这个方法中的username参数只能使用@PathVariable来接收,因为username参数是url的path上携带的参数。username是无法使用RequestParam来接受的。
testRequestParam这个方法只能用于
localhost:8080/api/v1/books2/12?name=java in action&author=ric&isbn=dsdas2334
这种模式的请求,因为RequestParam只能用于接收请求上带的params,testPathVariable是无法接收上面的name、author、isbn参数的。

2.内部参数不同

PathVariable有value,name,required这三个参数,而RequestParam也有这三个参数,并且比PathVariable多一个参数defaultValue

(该参数用于当请求体中不包含对应的参数变量时,参数变量使用defaultValue指定的默认值)

3.PathVariable一般用于get和delete请求,RequestParam一般用于post请求。

原文地址:https://www.cnblogs.com/kjitboy/p/12191251.html

时间: 2024-08-11 20:02:09

Spring MVC注解中@PathVariable和@RequestParam的区别的相关文章

springmvc中@PathVariable和@RequestParam的区别

http://localhost:8080/Springmvc/user/page.do?pageSize=3&pageNow=2 你可以把这地址分开理解,其中问号前半部分:http://localhost:8080/Springmvc/user/page.do 这个就是路径,是你的请求url,而如果这个路径上有数据匹配,用的就是@PathVariable  如 @RequestMapping(value="/page{pageNo}.do") public String pa

Spring MVC注解的一些案列

1.  spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quo

spring mvc 注解@Controller @RequestMapping @Resource的详细例子

现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过要想灵活运用Spring MVC来应对大多数的Web开发,就必须要掌握它的配置及原理. 一.Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar包引入 Spring 2.5.6:spring.jar.spring-webmvc.jar.comm

spring mvc(注解)上传文件的简单例子

spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationContext.xml中 <bean id=”multipartResolver” class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”/> 关于文件上传的配置不能少 大家可以看具体代码如下: web.xml &

Spring mvc注解说明

编号 注解 说明 位置 备注 1 @Controller 将类变成Spring Bean 类 现阶段 @Controller . @Service 以及 @Repository 和 @Component 注解的作用是等价的 2 @RequestMapping 请求映射 类.方法 标注在类上意指类实现 Controller 接口 标注在方法上意指扩展 Spring 预定义 Controller ( 如:SimpleFormController) 3 @RequestParam 入参绑定 URL 入

Spring MVC注解配置结合Hibernate的入门教程及其代码实例

原文:Spring MVC注解配置结合Hibernate的入门教程及其代码实例 源代码下载地址:http://www.zuidaima.com/share/1787210045197312.htm 1.概述 本文旨在搭建Spring MVC+Hibernate开发框架,通过一个简单的demo讲解Spring MVC的相关配置文件,以及通过注解方式实现简单功能. 开发框架:Spring+Spring MVC+Hibernate(Spring所用的版本为3.0.5). 数据库:MySQL(数据库名称

spring mvc controller中获取request head内容

spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public String print(@PathVariable Integer mlid, @PathVariable String ptn, @PathVariable String name, HttpSession session, Model model, @RequestHeader String referer,

spring mvc 注解入门示例

web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/ja

Spring MVC程序中得到静态资源文件css,js,图片文件的路径问题总结

上一篇 | 下一篇 Spring MVC程序中得到静态资源文件css,js,图片 文件的路径 用 Spring MVC 开发应用程序,对于初学者有一个很头疼的问题,那就是程序数据都已经查询出来了,但界面样式仍然十分丑陋,加载不了 css,js,图片等资源文件.当你在浏览器上直接输入某个css文件的路径时,直接得到404错误,而路径肯定没有错,其原因就在于在web.xml 中配置了类似如下的 spring servlet: 程序代码 <servlet>    <servlet-name&g