GET, post method, according to the request header Content-type value to judge:
- application/x-www-form-urlencoded, Optional (that is, not necessary, because the data of this situation @requestparam, @ModelAttribute can also be processed, of course @requestbody can also handle);
- Multipart/form-data, cannot be processed (i.e. using @requestbody cannot process data in this format);
- Other formats must be (other formats include Application/json, Application/xml, etc.). Data in these formats must be handled using @requestbody);
Code:
[Java]View PlainCopy
- Package Com.example.controller;
- Import org.springframework.web.bind.annotation.GetMapping;
- Import org.springframework.web.bind.annotation.PathVariable;
- Import Org.springframework.web.bind.annotation.RequestBody;
- Import org.springframework.web.bind.annotation.RequestMapping;
- Import Org.springframework.web.bind.annotation.RequestMethod;
- Import Org.springframework.web.bind.annotation.RequestParam;
- Import Org.springframework.web.bind.annotation.RestController;
- Import Com.example.bean.RequestLoginBean;
- Import Com.example.response.BaseResponse;
- Import Com.google.gson.Gson;
- @RestController
- @RequestMapping (value = "/index")
- Public class Login {
- /**
- * Index Home
- *
- * @return
- */
- @RequestMapping (value = "/home")
- Public String Home () {
- return "Index Home";
- }
- /**
- * Get 1 parameters
- *
- * @param name
- * User Name
- * @return Return results
- */
- @GetMapping (value = "/{name}")
- Public String Index (@PathVariable string name) {
- return "Oh you is" + name + "<br> Nice to meet you"; It doesn't work, so just use the tags in the HTML .
- }
- /**
- * Simple POST request
- *
- * @param name
- * @param pwd
- * @return
- */
- @RequestMapping (value = "/testpost", method = Requestmethod.post)
- Public String Testpost () {
- System.out.println ("Hello test post");
- return "OK";
- }
- /**
- * Get two parameters at the same time
- *
- * @param name
- * User Name
- * @param pwd
- * Password
- * @return Return results
- */
- @GetMapping (value = "/login/{name}&{pwd}")
- Public String Login (@PathVariable string name, @PathVariable string pwd) {
- if (name.equals ("admin") && pwd.equals ("admin")) {
- return "Hello welcome admin";
- } Else {
- return "Oh sorry user name or password is wrong";
- }
- }
- /**
- * Login via GET request
- *
- * @param name
- * @param pwd
- * @return
- */
- @RequestMapping (value = "/loginbyget", method = Requestmethod.get)
- Public string Loginbyget (@RequestParam (value = "Name", required = true) string name,
- @RequestParam (value = "pwd", required = true) String pwd) {
- return Login4return (name, PWD);
- }
- /**
- * Login via POST request
- *
- * @param name
- * @param pwd
- * @return
- */
- @RequestMapping (value = "/loginbypost", method = Requestmethod.post)
- Public string Loginbypost (@RequestParam (value = "Name", required = true) string name,
- @RequestParam (value = "pwd", required = true) String pwd) {
- System.out.println ("Hello post");
- return Login4return (name, PWD);
- }
- /**
- * The parameter is a Bean object. Spring automatically associates mappings for us
- * @param Loginbean
- * @return
- */
- @RequestMapping (value = "/loginbypost1", method = {requestmethod.post, requestmethod.get})
- Public String LoginByPost1 (Requestloginbean loginbean) {
- if (null! = Loginbean) {
- return Login4return (Loginbean.getname (), loginbean.getpwd ());
- } Else {
- return "error";
- }
- }
- /**
- * The request content is a JSON string, and spring will automatically match him to our parameter bean, but add @requestbody annotations.
- *
- * @param name
- * @param pwd
- * @return
- */
- @RequestMapping (value = "/loginbypost2", method = {requestmethod.post, requestmethod.get})
- Public String loginByPost2 (@RequestBody requestloginbean loginbean) {
- if (null! = Loginbean) {
- return Login4return (Loginbean.getname (), loginbean.getpwd ());
- } Else {
- return "error";
- }
- }
- /**
- * Ways to respond to logins
- *
- * @param name
- * User Name
- * @param pwd
- * Password
- * @return Return processing results
- */
- private String Login4return (string name, string pwd) {
- String result;
- Baseresponse response = new Baseresponse ();
- if (name.equals ("admin") && pwd.equals ("admin")) {
- result = "Hello welcome admin";
- Response.setstate (true);
- } Else {
- result = "Oh sorry user name or password is wrong";
- Response.setstate (false);
- }
- System.out.println ("received request, request result:" + result);
- return new Gson (). ToJson (response);
- }
- }
Spring boot Get and post requests, and requestbody for JSON string processing