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请求。

猜你喜欢

转载自www.cnblogs.com/kjitboy/p/12191251.html