获取请求参数

1、Get请求获取参数

@RestController
@RequestMapping("/req")
public class ReqController {

    /**
     * @RequestParam用参数接收 形参默认key一样
     * @RequestParam(name = "name")   或者  @RequestParam(value = "name")  与传的key一样
     * @RequestParam(value = "30")    默认值30
     * @RequestParam(required = false) String age    age不用必传,默认为必需
     * @param
     * @param age
     */
    @GetMapping("/get1")
    public void getPath(@RequestParam(value = "name") String name1111, @RequestParam(required = false) String age){
        System.out.println(name1111);
        System.out.println(age);
    }

    /**
     * 直接用参数接收,参数与key必须一样
     * @param name
     * @param age
     */
    @GetMapping("/get2")
    public void getPath2(String name,String age){
        System.out.println(name);
        System.out.println(age);
    }

    /**
     * 用requset接收  request.getQueryString()  返回name=zhangsan&age=20
     * @param request
     */
    @GetMapping("/get3")
    public void getPathForRequest(HttpServletRequest request){
        System.out.println(request.getQueryString());
    }

    /**
     * request.getParameter('参数名称')
     * @param request
     */
    @GetMapping("/get4")
    public void getPathForRequestPara(HttpServletRequest request){
        System.out.println(request.getParameter("name"));
        System.out.println(request.getParameter("age"));
    }

    /**
     * 用对象接收
     * @param user
     */
    @GetMapping("/get5")
    public void getPathForObj(User user){
        System.out.println(user.getName());
        System.out.println(user.getAge());
    }

    /**
     * Restful风格   127.0.0.1/req/get6/zhangsan
     * @param name
     */
    @GetMapping("/get6/{name}")
    public void getForPath(@PathVariable String name){
        System.out.println(name);
    }
}

2、Post请求获取参数

/**
     * post  localhost/req/post1?name=zhangsan&age=20  params路径方式
     * post  body form-data
     *
     * @param name
     * @param age
     * @RequestParam
     */
    @PostMapping("/post1")
    public void postForForm(@RequestParam String name, @RequestParam int age) {
        System.out.println(name);
        System.out.println(age);
    }


    /**
     * @RequestParam Map<String, Object> map  接收表单全部数据
     * @param map
     */
    @PostMapping("/post2")
    public void postForForm2(@RequestParam Map<String, Object> map) {
        System.out.println(map);
    }


    /**
     * @param json
     * @RequestBody 接收json数据
     */
    @PostMapping("/post3")
    public void postForJson(@RequestBody JSONObject json) {
        System.out.println(json);
        Object materialModel = json.get("title");
        System.out.println(materialModel);

        JSONArray periods1 = json.getJSONArray("jsonArray");
        System.out.println(periods1);

        List<Dict> dicts = JSONUtil.toList(periods1, Dict.class);
        for (int i = 0; i < dicts.size(); i++) {
            System.out.println(dicts.get(i));
        }
    }


    /**
     * 从request中获取请求体json数据
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @PostMapping("/post4")
    public String test(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println(request.getRequestURL());
        BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
        StringBuilder sb = new StringBuilder();
        String inputStr;
        while ((inputStr = streamReader.readLine()) != null) {
            sb.append(inputStr);
        }
        String body = sb.toString();
        JSONObject json = JSONUtil.parseObj(body);
        System.out.println(body);
        System.out.println(json);

        return "";
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值