spring boot get和post請求,以及requestbody為json串時候的處理


GET、POST方式提時, 根據request header Content-Type的值來判斷:

  •     application/x-www-form-urlencoded, 可選(即非必須,因為這種情況的數據@RequestParam, @ModelAttribute也可以處理,當然@RequestBody也能處理);
  •     multipart/form-data, 不能處理(即使用@RequestBody不能處理這種格式的數據);
  •     其他格式, 必須(其他格式包括application/json, application/xml等。這些格式的數據,必須使用@RequestBody來處理);
代碼:
 
[java] view plain copy
  1. package com.example.controller;  
  2.   
  3. import org.springframework.web.bind.annotation.GetMapping;  
  4. import org.springframework.web.bind.annotation.PathVariable;  
  5. import org.springframework.web.bind.annotation.RequestBody;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RequestMethod;  
  8. import org.springframework.web.bind.annotation.RequestParam;  
  9. import org.springframework.web.bind.annotation.RestController;  
  10.   
  11. import com.example.bean.RequestLoginBean;  
  12. import com.example.response.BaseResponse;  
  13. import com.google.gson.Gson;  
  14.   
  15. @RestController  
  16. @RequestMapping(value = "/index")  
  17. public class Login {  
  18.   
  19.     /** 
  20.      * index home 
  21.      *  
  22.      * @return 
  23.      */  
  24.     @RequestMapping(value = "/home")  
  25.     public String home() {  
  26.         return "index home";  
  27.     }  
  28.       
  29.     /** 
  30.      * 得到1個參數 
  31.      *  
  32.      * @param name 
  33.      *            用戶名 
  34.      * @return 返回結果 
  35.      */  
  36.     @GetMapping(value = "/{name}")  
  37.     public String index(@PathVariable String name) {  
  38.         return "oh you are " + name + "<br> nice to meet you";// \n不起作用了,那就直接用html中的標簽吧  
  39.     }  
  40.   
  41.     /** 
  42.      * 簡單post請求 
  43.      *  
  44.      * @param name 
  45.      * @param pwd 
  46.      * @return 
  47.      */  
  48.     @RequestMapping(value = "/testpost", method = RequestMethod.POST)  
  49.     public String testpost() {  
  50.         System.out.println("hello  test post");  
  51.         return "ok";  
  52.     }  
  53.   
  54.     /** 
  55.      * 同時得到兩個參數 
  56.      *  
  57.      * @param name 
  58.      *            用戶名 
  59.      * @param pwd 
  60.      *            密碼 
  61.      * @return 返回結果 
  62.      */  
  63.     @GetMapping(value = "/login/{name}&{pwd}")  
  64.     public String login(@PathVariable String name, @PathVariable String pwd) {  
  65.         if (name.equals("admin") && pwd.equals("admin")) {  
  66.             return "hello welcome admin";  
  67.         } else {  
  68.             return "oh sorry user name or password is wrong";  
  69.         }  
  70.     }  
  71.   
  72.     /** 
  73.      * 通過get請求去登陸 
  74.      *  
  75.      * @param name 
  76.      * @param pwd 
  77.      * @return 
  78.      */  
  79.     @RequestMapping(value = "/loginbyget", method = RequestMethod.GET)  
  80.     public String loginByGet(@RequestParam(value = "name", required = true) String name,  
  81.             @RequestParam(value = "pwd", required = true) String pwd) {  
  82.         return login4Return(name, pwd);  
  83.     }  
  84.   
  85.     /** 
  86.      * 通過post請求去登陸 
  87.      *  
  88.      * @param name 
  89.      * @param pwd 
  90.      * @return 
  91.      */  
  92.     @RequestMapping(value = "/loginbypost", method = RequestMethod.POST)  
  93.     public String loginByPost(@RequestParam(value = "name", required = true) String name,  
  94.             @RequestParam(value = "pwd", required = true) String pwd) {  
  95.         System.out.println("hello post");  
  96.         return login4Return(name, pwd);  
  97.     }  
  98.   
  99.     /** 
  100.      * 參數為一個bean對象.spring會自動為我們關聯映射 
  101.      * @param loginBean 
  102.      * @return 
  103.      */  
  104.     @RequestMapping(value = "/loginbypost1", method = { RequestMethod.POST, RequestMethod.GET })  
  105.     public String loginByPost1(RequestLoginBean loginBean) {  
  106.         if (null != loginBean) {  
  107.             return login4Return(loginBean.getName(), loginBean.getPwd());  
  108.         } else {  
  109.             return "error";  
  110.         }  
  111.     }  
  112.       
  113.     /** 
  114.      * 請求內容是一個json串,spring會自動把他和我們的參數bean對應起來,不過要加@RequestBody注解 
  115.      *  
  116.      * @param name 
  117.      * @param pwd 
  118.      * @return 
  119.      */  
  120.     @RequestMapping(value = "/loginbypost2", method = { RequestMethod.POST, RequestMethod.GET })  
  121.     public String loginByPost2(@RequestBody RequestLoginBean loginBean) {  
  122.         if (null != loginBean) {  
  123.             return login4Return(loginBean.getName(), loginBean.getPwd());  
  124.         } else {  
  125.             return "error";  
  126.         }  
  127.     }  
  128.   
  129.       
  130.   
  131.   
  132.     /** 
  133.      * 對登錄做出響應處理的方法 
  134.      *  
  135.      * @param name 
  136.      *            用戶名 
  137.      * @param pwd 
  138.      *            密碼 
  139.      * @return 返回處理結果 
  140.      */  
  141.     private String login4Return(String name, String pwd) {  
  142.         String result;  
  143.         BaseResponse response = new BaseResponse();  
  144.         if (name.equals("admin") && pwd.equals("admin")) {  
  145.             result = "hello welcome admin";  
  146.             response.setState(true);  
  147.         } else {  
  148.             result = "oh sorry user name or password is wrong";  
  149.             response.setState(false);  
  150.         }  
  151.         System.out.println("收到請求,請求結果:" + result);  
  152.         return new Gson().toJson(response);  
  153.     }  
  154. }  



免責聲明!

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



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