1. 程式人生 > >@RequestParam和@RequestBody的區別

@RequestParam和@RequestBody的區別

一、@RequestParam使用

使用@RequestParm用於繫結controller上的引數,可以是多個引數,也可以是一個Map集合,GET,POST均可

@PostMapping(value = "requestParam")
@ResponseBody
public Boolean requestParam(@RequestParam(name = "string",required = false) String str,@RequestParam(name = "integer",defaultValue = "123456") int integer){
    System.out
.println("str:"+str); System.out.println("integer:"+integer); return true; }

@RequestParm中name屬性是指定引數名,required屬性預設為ture,表示必傳。若為false則為非必傳。屬性有defaultValue預設值選項,若該引數為null時,會將預設值填充到引數上。

image

@PostMapping(value = "paraMap")
@ResponseBody
public Map paraMap(@RequestParam Map<String, String
> map){ System.out.println("map name:"+map); return map; }

還可以傳入map集合
image

最後說一下使用@RequestParam的要求
- 均支援POST,GET請求
- 只支援Content-Type: 為 application/x-www-form-urlencoded編碼的內容。Http協議中,如果不指定Content-Type,則預設傳遞的引數就是application/x-www-form-urlencoded型別)

二、@RequestBody

@RequestBody繫結一個物件實體

@PostMapping(value
= "requestBody") @ResponseBody public User requestBody(@RequestBody User user){ System.out.println("user:"+user.getName()); return user; }

image
- 不支援get請求,因為get請求沒有HttpEntity
- 必須要在請求頭中申明content-Type(如application/json).springMvc通過HandlerAdapter配置的HttpMessageConverters解析httpEntity的資料,並繫結到相應的bean上
- 只能一個@RequestBody。且不能與@RequestParam一起使用

三、總結

區別 @RequestParam @RequestBody
content-type 僅支援x-www-form-urlencoded 支援json格式
請求型別 ALL 除了GET
註解個數 可多個 只能一個

相關推薦

spring boot的@RequestParam@RequestBody區別

ble rest val LV 請求頭 ade head 暴露 設計 尊重原創:https://blog.csdn.net/u013306545/article/details/79071683 一、問題描述 由於項目是前後端分離,因此後臺使用的是spring

@RequestParam @RequestBody區別

@RequestParam Annotation which indicates that a method parameter should be bound to a web request parameter. HTTP請求引數 比如 http://xx.com/

@RequestParam@RequestBody區別

一、@RequestParam使用 使用@RequestParm用於繫結controller上的引數,可以是多個引數,也可以是一個Map集合,GET,POST均可 @PostMapping(value = "requestParam") @Respons

@RequestParam@RequestBody使用場景

spring的RequestParam註解接收的引數是來自於requestHeader中,即請求頭,也就是在url中,格式為xxx?username=123&password=456,而RequestBody註解接收的引數則是來自於requestBody中,即請求體中。 如果為get

springMVC中@RequestParam@RequestBody的作用

  @RequestParam和@RequestBody是什麼區別,估計很多人還是不太清楚, 因為一般用@ RequestParam就足夠傳入引數了,要說他們區別,就需要知道contentType是什麼? Content-Type: 預設為 application/x-www-form-url

ajax同時傳輸資料供requestparamrequestbody使用

最近在專案中碰到一個問題,需要同時傳輸兩個flag和一個xml檔案。後端請求的是requestParam和requestBody兩個,兩者的區別在於是否是 application/x-www-form-

淺談@RequestParam與@RequestBody區別

@RequestParam:與servlet的request.getParameter()用法一致,都是用來接收form表單的提交,預設是application/x-www-form-urlencoded編碼的內容,我們平時的jQuery與js提交都是預設applicatio

@RequestParam、@RequestBody@ModelAttribute區別

一、@RequestParam GET和POST請求傳的引數會自動轉換賦值到@RequestParam 所註解的變數上 1. @RequestParam(org.springframework.web.bind.annotation.RequestParam)用於將指定的

淺談@RequestMapping @ResponseBody @RequestBody 註解的用法與區別

ber attribute thrown text 返回結果 mode 需要 oca 格式 1.@RequestMapping 國際慣例先介紹什麽是@RequestMapping,@RequestMapping 是一個用來處理請求地址映射的註解,可用於類或方法上。用於類上,

SpringMVC中@PathVariable@RequestParam之間的區別

@PathVariable繫結URI模板變數值 @PathVariable是用來獲得請求url中的動態引數的 @PathVariable用於將請求URL中的模板變數對映到功能處理方法的引數上。//通俗來講配置url和方法的一個關係 @RequestMapping("/item/{item

@RequestParam@ResponseBody註解的區別

@RequestParam 用來處理Content-Type: 為 application/x-www-form-urlencoded編碼的內容。(Http協議中,如果不指定Content-Type,則預設傳遞的引數就是application/x-www-form-urlencoded型別) Reques

@RequestMapping @ResponseBody @RequestBody 用法與區別

[email protected] 國際慣例先介紹什麼是@RequestMapping,@RequestMapping 是一個用來處理請求地址對映的註解,可用於類或方法上。用於類上,表示類中的所有響應請求的方法都是以該地址作為父路徑;用於方法上,表示在類的父路徑下追加方法上註解中

@RequestParam不用的區別以及 @PathVariable 的區別,簡單易懂

@RequestParam(將請求引數繫結到方法引數) 1:簡單地說,不用的話,前臺jsp頁面的引數名稱必須和此處方法的形參名字一樣: 比如:前臺是userId @RequestMapping(“/test2”) public String test2(int userId, i

SSM框架之@ResponseBody@RequestBody區別及HttpMessageConverter的作用

一、@ResponseBody @ResponseBody作用於方法上,將返回結果直接以Json等格式返回,直接封裝到response物件的Body中,不經過SpringMVC的檢視解析器。一般用於前後端分離的專案,或者Ajax等非同步請求。 @ResponseBody public Stu

詳述 @ResponseBody @RequestBody 註解的區別

1 前言 在詳述 @ResponseBody 和 @RequestBody 註解之前,咱先了解一下 @RequestMapping 註解,@RequestMapping 是一個用來處理請求地址對映的註解,可用於類或方法上。用於類上,表示類中的所有響應請求的方法

GETPOST區別總結

get 、post 、區別一、GET和POST區別的普遍看法:HTTP 定義了與服務器交互的不同方法,最常用的有4種,Get、Post、Put、Delete,如果我換一下順序就好記了,Put(增),Delete(刪),Post(改),Get(查),即增刪改查,下面簡單敘述一下:1)Get, 它用於獲取信息,註

JS中const、varlet區別

方法 pre 命令 con 使用 它的 comm 作用 影響 在JavaScript中有三種聲明變量的方式:var、let、const。 1.const 聲明創建一個只讀的常量。這不意味著常量指向的值不可變,而是變量標識符的值只能賦值一次,必須初始化。 const b

equals == 的區別

strong 都是 什麽 brush -s 新的 equals方法 實現 繼承 首先 看比較的對象是否為字符串,若為(String)字符串用equals 比較, 比較的是他們的值。相同返回 true ,不相同返回false. package one; p

mybatis中的#$的區別

背景 插入 trac sql註入 -m .com article 參數 -s 1. #將傳入的數據都當成一個字符串,會對自動傳入的數據加一個雙引號。如:order by #user_id#,如果傳入的值是111,那麽解析成sql時的值為order by "111", 如果傳

hibernate中hql語句中listiterate區別

每次 hibernate 寫入 所有 讀取 條件 iter 查詢 hql 1.使用list()方法獲取查詢結果,每次發出一條語句,獲取全部數據。2.使用iterate()方法獲取查詢結果,先發出一條SQL語句用來查詢滿足條件數據的id,然後依次按照這些id查詢記錄,也就是要