spring 接收数组、哈希请求参数

PHP HTTP请求

在PHP里接收REQUEST数据是件很容易的事,按我略懂的开发语言里,PHP是使用最简单方便的。
$_POST、$_GET、$_REQUEST 几个全局变量;

<input type=”text” name=”username[]” value=”jeftom”>
<input type=”text” name=”username[]” value=”刘德华”>

$_POST[‘username[]’] 可以获取到表单提交过来的 field,是一个数组格式。

Spring 框架HTTP请求

@RequestParam注解是获取静态URL传入的参数
@PathVariable是获取请求路径中的变量作为参数

@RestController
@RequestMapping(value="users")
public class UserController {
    /**
     * 用户列表
     * @type {String}
     */
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> list(@RequestParam Long user_id) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("user_id", user_id);
        return map;
    }

    /**
     * 获取单个用户信息
     * @type {String}
     */
    @RequestMapping(value = "/user/{user_id}", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> get_user(@PathVariable Long user_id) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("user_id", user_id);
        return map;
    }
}
多个input同名数组
<form method="post" action="/username">
    <input type="text" name="username[]" value="jeftom">
    <input type="text" name="username[]" value="刘德华">
    <input type="submit" value="submit">
</form>

多个input同名不同数字下标
<form method="post" action="/fuck">
    <input type="text" name="username[0]" value="jeftom">
    <input type="text" name="username[1]" value="刘德华">
    <input type="submit" value="submit">
</form>

多个input同名
<form method="post" action="/index">
    <input type="text" name="username" value="jeftom">
    <input type="text" name="username" value="刘德华">
</form>
package com.jeftom.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(value="用户相关的接口")
@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping(value = "/username", method = RequestMethod.POST)
    @ResponseBody
    public String username(@RequestParam(required = true, defaultValue = "", value = "username[]") List<String> username) {
        System.out.println(username.toString());

        return "ok";
    }

    @RequestMapping(value = "/fuck", method = RequestMethod.POST)
    @ResponseBody
    //  这里不能用 value,否则会报错
    public String fuck(@RequestParam(required = true) Map<String, Integer> username) {
        System.out.println(username.toString());

        return "ok";
    }

    /**
     * URL http://localhost:8081/user/fuck2?params[]=22&params[]=11
     * result: [22, 11]
     */
    @RequestMapping(value = "fuck2", method = RequestMethod.POST)
    public String fuck2(@RequestParam(value = "params[]", required = false) String[] params) {
        System.out.println(Arrays.toString(params));

        return "OK";
    }

    /**
     * 使用 string 接收到是逗号分隔的字符串
     * result: aa,bb
     */
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    @ResponseBody
    public String index(HttpServletRequest request, HttpServletResponse response, String username) {
        System.out.println(username);
        return "OK";
    }

    /**
     * 将request中的所有参数存放到自定义的map中
     * 
     * @return Map<result>
     * @throws Exception 
     */
    public Map<String, Object> getParameterMap(HttpServletRequest request) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, String[]> tempMap = request.getParameterMap();
        Set<String> keys = tempMap.keySet();
        for (String key : keys) {
            byte source [] = request.getParameter(key).getBytes("iso8859-1");
            String modelname = new String (source, "UTF-8");
            result.put(key,modelname);
        }
        return result;
    }
}

也就是说,用 String username 接收到的是逗号连接的字符串,用 String[] username 接收到的是一个数组;不像PHP里一样需要在 name 加上 “[]” 才是数组。

参考文章:

https://blog.csdn.net/qwe6112071/article/details/51062317
https://blog.csdn.net/haiyoung/article/details/80715094
https://blog.csdn.net/do_bset_yourself/article/details/51314152
https://www.cnblogs.com/wangchuanfu/p/5913310.html
https://www.jianshu.com/p/b4c8d8de4dad
https://ask.csdn.net/questions/699122
https://blog.csdn.net/eeeeasy/article/details/81708600

发表评论