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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM