In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How does Controller receive parameters in SpringBoot2? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Address value
@ PathVariable
Gets the path parameters. This form is url/ {id}.
? Transfer value
@ RequestParam
Gets the query parameters. In the form of url?name=
Use the annotation @ RequestParam to bind the request parameter to the method input parameter
An exception occurs when the request parameter username does not exist, which can be solved by setting the property required=false, for example: @ RequestParam (value= "username", required=false)
Body parameter
/ / application/jason
@ PostMapping (path = "/ demo1")
Public void demo1 (@ RequestBody Person person) {
No comments (form submission)
/ / form-data
@ PostMapping (path = "/ demo1")
Public void demo1 (Person person) {
Request header parameters and Cookie
@ RequestHeader
2. @ CookieValue
Examples
Java Code:
@ GetMapping ("/ demo3")
Public void demo3 (@ RequestHeader (name = "myHeader") String myHeader
@ CookieValue (name = "myCookie") String myCookie) {
System.out.println ("myHeader=" + myHeader)
System.out.println ("myCookie=" + myCookie)
You can do that, too.
@ GetMapping ("/ demo3")
Public void demo3 (HttpServletRequest request) {
System.out.println (request.getHeader ("myHeader"))
For (Cookie cookie: request.getCookies ()) {
If ("myCookie" .equals (cookie.getName () {
System.out.println (cookie.getValue ())
/ / request header parameters and Cookie
@ RequestMapping ("request5")
Public String test5 (@ RequestHeader (name = "Header") String Header
@ CookieValue (name = "cookie1") String cookie1
@ CookieValue (name = "cookie2") String cookie2) {
Return "Header:" + Header+ "cookie1:" + cookie1+ "cookie2" + cookie2
}
/ / Cookie
@ GetMapping ("/ ee")
Public String ee (@ RequestHeader (name= "myHeader") String myHeader,@CookieValue (name= "myCookie") String myCookie) {
System.out.println ("myHeader=" + myHeader)
System.out.println ("myCookie=" + myCookie)
Return "-" + myHeader+ "=" + myCookie
}
The parameters of the form are written in the formal parameters of the corresponding method of Controller
It applies to get submission, but not to post submission.
/ * *
* 1. Write the parameters of the form directly in the parameters of the corresponding method in Controller
* @ param username
* @ param password
* @ return
, /
@ RequestMapping ("/ addUser1")
Public String addUser1 (String username,String password) {
System.out.println ("username is:" + username)
@ ModelAttribute annotation to get FORM form data of POST request
/ * *
* use @ ModelAttribute annotations to get FORM form data for POST requests
* @ param user
* @ return
, /
@ RequestMapping (value= "/ addUser5", method=RequestMethod.POST)
Public String addUser5 (@ ModelAttribute ("user") UserModel user) {
System.out.println ("username is:" + user.getUsername ())
System.out.println ("password is:" + user.getPassword ())
Return "demo/index"
Test code
Controller
Package com.zz.controller
Import java.util.HashMap
Import java.util.Map
Import com.zz.entity.User
Import org.springframework.web.bind.annotation.*
@ RestController
@ RequestMapping ("test")
Public class TestController {
/ / pass a value by path
@ RequestMapping ("T1 / {p1}")
Public Map T1 (@ PathVariable ("p1") String paramter1) {
Map map=new HashMap ()
Map.put ("rs", paramter1)
Return map
}
/ /? Transfer value
@ RequestMapping ("T2")
Public Map T2 (@ RequestParam ("p1") String paramter1)
{
System.out.println (paramter1)
Map map=new HashMap ()
Map.put ("rs", paramter1)
Return map
}
/ / Body parameter
/ / application/jason
@ RequestMapping ("T3")
Public Map T3 (@ RequestBody User person) {
Map map=new HashMap ()
Map.put ("rs", person.getName ())
Return map
}
/ / No comments (submitted by form)
/ / form-data
@ RequestMapping ("T4")
Public Map T4 (User person) {
Map map=new HashMap ()
Map.put ("rs", person.getName ())
Return map
}
/ / request header parameters and Cookie
@ RequestMapping ("T5")
Public Map T5 (@ RequestHeader (name = "myHeader") String myHeader
@ CookieValue (name = "myCookie") String myCookie) {
System.out.println ("myHeader=" + myHeader)
System.out.println ("myCookie=" + myCookie)
Map map=new HashMap ()
Map.put ("rs", myHeader)
Return map
}
/ / the parameters of the form are written in the formal parameters of the corresponding method of Controller
/ / it is applicable to get submission, not post submission.
@ RequestMapping ("T6")
Public Map T6 (String name,String pwd) {
Map map=new HashMap ()
Map.put ("rs", name)
Return map
}
}
HTML
Bootstrap instance
test
T1
T2
T3
T4
T5
T6
$(document) .ready (function () {
/ / write your code here.
$("# T1") .click (function () {
$.get ("test/t1/jacky", function (data) {
Alert ("Data Loaded:" + data.rs)
});
})
$("# T2") .click (function () {
$.get ("test/t2?p1=jerry", function (data) {
Alert ("Data Loaded:" + data.rs)
});
})
$("# T3") .click (function () {
Var data = {"name": "petter", "pwd": "123"}
$.ajax ({
Url: "test/t3"
Async: false
Type: "POST"
ContentType: 'application/json'
DataType: 'json'
Data: JSON.stringify (data)
Success: function (data) {
Alert ("Data Loaded:" + data.rs)
}
});
})
$("# T4") .click (function () {
Var data = {"name": "petter", "pwd": "123"}
$.ajax ({
Url: "test/t4?name=jacky"
Async: false
Type: "POST"
ContentType: 'form-data'
Success: function (data) {
Alert ("Data Loaded:" + data.rs)
}
});
})
/ / not available for the time being
$("# T5") .click (function () {
Var data = {"name": "petter", "pwd": "123"}
$.ajax ({
Url: "test/t5"
Async: false
Type: "POST"
ContentType: 'application/json'
DataType: 'json'
Headers: {
'Header': "from header"
'Cookie':'sdfs'
}
Data: JSON.stringify (data)
Success: function (data) {
Alert ("Data Loaded:" + data.rs)
}
});
})
$("# T6") .click (function () {
$.get ("test/t6?name=jerry&pwd=234", function (data) {
Alert ("Data Loaded:" + data.rs)
});
})
});
SpringBoot Form post failed to submit a solution to the jump
Error log: tSupportedException: Request method 'POST' not supported
Solution: return "redirect:/index/toIndex" in the post controller method; execute the jump controller of get
Package com.zz.controller;/**
* @ Description: description
* @ Author: Bsea
* @ CreateDate: ${Date}
, /
Import com.zz.config.DataValidationException
Import com.zz.entity.Member
Import com.zz.form.MemberForm
Import com.zz.service.MemberService
Import com.zz.util.FormUtil
Import com.zz.util.ResultVOUtil
Import com.zz.vo.ResultVO
Import io.swagger.annotations.Api
Import io.swagger.annotations.ApiImplicitParam
Import io.swagger.annotations.ApiImplicitParams
Import io.swagger.annotations.ApiOperation
Import org.springframework.beans.BeanUtils
Import org.springframework.data.domain.Page
Import org.springframework.stereotype.Controller
Import org.springframework.validation.BindingResult
Import org.springframework.web.bind.annotation.*
Import javax.annotation.Resource
Import javax.validation.Valid
/ * *
* @ Description: java class function description
* @ Author: Bsea
* @ CreateDate: 2019-10-16 $21:27 $
, /
@ Api (value = "member Controller")
@ Controller
@ RequestMapping ("index")
Public class IndexController {
@ Resource
MemberService memberService
@ ApiOperation (value = "add member", notes = "find member by name")
@ ApiImplicitParam (name = "name", value = "member name", required = true, dataType = "String", paramType = "path")
@ PostMapping ("add")
Public String add (@ Valid MemberForm member, BindingResult bindingResult) {
System.out.println (member)
If (bindingResult.hasErrors ()) {
Throw new DataValidationException ("validation error")
}
Member member1=new Member ()
BeanUtils.copyProperties (member,member1)
If (memberService.add (member1)! = null) {
Return "redirect:/index/toIndex"
} else {
Return "redirect:/index/toIndex"
}
}
@ RequestMapping ("toIndex")
Public String toIndex () {
Return "/ index2.html"
}
}
The answer to the question about how Controller receives parameters in SpringBoot2 is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.