@RequestBody 和 @RequestParam可以同时使用
@RequestParam和@RequestBody這兩個(gè)注解是可以同時(shí)使用的。
網(wǎng)上有很多博客說@RequestParam 和@RequestBody不能同時(shí)使用,這是錯(cuò)誤的。根據(jù)HTTP協(xié)議,并沒有說post請(qǐng)求不能帶URL參數(shù),經(jīng)驗(yàn)證往一個(gè)帶有參數(shù)的URL發(fā)送post請(qǐng)求也是可以成功的。只不過,我們?nèi)粘i_發(fā)使用GET請(qǐng)求搭配@RequestParam,使用POST請(qǐng)求搭配@RequestBody就滿足了需求,基本不怎么同時(shí)使用二者而已。
? 自己個(gè)人實(shí)際驗(yàn)證結(jié)果:
package com.example.model;import java.util.List;public class PramInfo {public int getId() {return id;}public void setId(int id) {this.id = id;}public String getStr() {return str;}public void setStr(String str) {this.str = str;}private int id;private String str;}package com.example.demo;import com.example.model.PramInfo; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;@RestController @RequestMapping(value = "/test") public class TestContoller {@RequestMapping(value = "/dxc")public String print(PramInfo info) {return info.getId() + ": :" + info.getStr();}@RequestMapping(value = "/getUserJson")public String getUserJson(@RequestParam(value = "id") int id, @RequestParam(value = "name2", required = false) String name2, @RequestBody PramInfo pramInfo) {return (id + "--" + name2 + ";paramInfo:" + pramInfo.getStr() + ";pramInfo.id:" + pramInfo.getId());} }在postman發(fā)送如下post請(qǐng)求,返回正常:
body中參數(shù)如下:
? 從結(jié)果來看,post請(qǐng)求URL帶參數(shù)是沒有問題的,所以@RequestParam和@RequestBody是可以同時(shí)使用的【經(jīng)測(cè)試,分別使用Postman 和 httpClient框架編程發(fā)送http請(qǐng)求,后端@RequestParam和@RequestBody都可以正常接收請(qǐng)求參數(shù),所以個(gè)人認(rèn)為可能一些前端框架不支持或者沒必要這么做,但是不能說@RequestParam和@RequestBody 不能同時(shí)使用】。
值得注意的地方:
1、postman的GET請(qǐng)求是不支持請(qǐng)求body的;
2、
@GetMapping(value = "/dxc")public String print(PramInfo info) {return info.getId() + ": :" + info.getStr();}這種請(qǐng)求方式,不加@RequestParam注解,也能直接取出URL后面的參數(shù),即參數(shù)可以與定義的類互相自動(dòng)轉(zhuǎn)化。
3、
@PostMapping(value = "/getUserJson")public String getUserJson(@RequestParam(value = "id") int id, @RequestParam(value = "name2", required = false) String name2, @RequestBody PramInfo pramInfo) {return (id + "--" + name2 + ";paramInfo:" + pramInfo.getStr() + ";pramInfo.id:" + pramInfo.getId());如果請(qǐng)求中的body沒有ParamInfo對(duì)象對(duì)應(yīng)的json串,即當(dāng)body為空(連json串的{}都沒有)時(shí),會(huì)報(bào)請(qǐng)求body空異常:
2018-05-12 23:45:27.494 WARN 6768 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.Stringcom.example.demo.TestContoller.getUserJson(int,java.lang.String,com.example.model.PramInfo) -05-12 23:45:27.494 WARN 6768 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.String com.example.demo.TestContoller.getUserJson(int,java.lang.String,com.example.model.PramInfo) 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的@RequestBody 和 @RequestParam可以同时使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。