zoukankan      html  css  js  c++  java
  • 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来处理);
    代码:
     
    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";//  不起作用了,那就直接用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. }  


  • 相关阅读:
    Android设置RadioButton在文字的右边
    如何创建启动界面Splash Screen
    sqlite3 数据类型 批量插入
    为PopupWindow设置弹出动画效果
    android activity生命周期
    Eclipse快捷键大全
    SQLite的使用
    创建窗口式Activity
    Android中实现按钮自动隐藏
    android技术片段
  • 原文地址:https://www.cnblogs.com/xuyatao/p/8296095.html
Copyright © 2011-2022 走看看