mvc的controller的传参和获参问题(post和get请求参数获取的区别)

application/json

ajax请求中content-type:application/json代表参数以json字符串传递给后台,controller接收需要@RequestBody 接收参数 例如:@RequestBody Map<String, Object> map,也可以使用类接收@RequestBody User user

application/x-www-form-urlencoded

ajax请求中content-type:application/x-www-form-urlencoded代表参数以键值对传递给后台,controller接收可以单个参数接收,如@RequestParam(“param”) String param;也可以用类接收User user,\color{#FF0000}{参数名需要一一对应}

原文地址:https://www.jianshu.com/p/b0d6504c7ccb

1、路径传参


eg:http://127.0.0.1:9999/earthquakeWarning/selectOne?id=6
获取代码:

package com.example.dtest.easyCode.controller;

import com.example.dtest.easyCode.entity.EarthquakeWarning;
import com.example.dtest.easyCode.service.EarthquakeWarningService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (EarthquakeWarning)表控制层
 *
 * @author makejava
 * @since 2021-05-21 09:07:11
 */
@RestController
@RequestMapping("earthquakeWarning")
public class EarthquakeWarningController {
    /**
     * 服务对象
     */
    @Resource
    private EarthquakeWarningService earthquakeWarningService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public EarthquakeWarning selectOne(Integer id) {
        System.out.println(id);
        EarthquakeWarning earthquakeWarning = new EarthquakeWarning();
        earthquakeWarning = earthquakeWarningService.queryById(id);
        System.out.println(earthquakeWarning);

        return this.earthquakeWarningService.queryById(id);
    }

}

2、路径传参


http://127.0.0.1:9999/earthquakeWarning/selectOne/6

接收代码:

package com.example.dtest.easyCode.controller;

import com.example.dtest.easyCode.entity.EarthquakeWarning;
import com.example.dtest.easyCode.service.EarthquakeWarningService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (EarthquakeWarning)表控制层
 *
 * @author makejava
 * @since 2021-05-21 09:07:11
 */
@RestController
@RequestMapping("earthquakeWarning")
public class EarthquakeWarningController {
    /**
     * 服务对象
     */
    @Resource
    private EarthquakeWarningService earthquakeWarningService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne/{id}")
    public EarthquakeWarning selectOne(@PathVariable Integer id) {
        System.out.println(id);
        EarthquakeWarning earthquakeWarning = new EarthquakeWarning();
        earthquakeWarning = earthquakeWarningService.queryById(id);
        System.out.println(earthquakeWarning);

        return this.earthquakeWarningService.queryById(id);
    }

}

3、post表单传参


http://127.0.0.1:9999/earthquakeWarning/selectOne
{“ids”:6}
在这里插入图片描述
接收代码:

package com.example.dtest.easyCode.controller;

import com.example.dtest.easyCode.entity.EarthquakeWarning;
import com.example.dtest.easyCode.service.EarthquakeWarningService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (EarthquakeWarning)表控制层
 *
 * @author makejava
 * @since 2021-05-21 09:07:11
 */
@RestController
@RequestMapping("earthquakeWarning")
public class EarthquakeWarningController {
    /**
     * 服务对象
     */
    @Resource
    private EarthquakeWarningService earthquakeWarningService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public EarthquakeWarning selectOne(@RequestBody EarthquakeWarning earthquakeWarning) {
       Integer id = earthquakeWarning.getIds();

        return this.earthquakeWarningService.queryById(id);
    }

}

表单传参特殊情况x-www-form-urlencoded

在这里插入图片描述

x-www-form-urlencoded post

post请求可以使用@ModelAttribute这个注解或者不加注解从而得到值
1、

package com.example.test_1.controller;

import com.example.test_1.entity.TestVO;
import org.springframework.web.bind.annotation.*;

@RestController
public class TestGet01 {

    @RequestMapping(value = "/test",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public String test(TestVO testVO){
        System.out.println(testVO);
        return "true";
    }


}

2、

package com.example.test_1.controller;

import com.example.test_1.entity.TestVO;
import org.springframework.web.bind.annotation.*;

@RestController
public class TestGet01 {

    @RequestMapping(value = "/test",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public String test(@ModelAttribute TestVO testVO){
        System.out.println(testVO);
        return "true";
    }


}

在这里插入图片描述
在这里插入图片描述

但是get请求却不能得到实体类的值**

x-www-form-urlencoded get

当为get请求时,@ModelAttribute或者不加注解(不管是x-www-from-urlencoded还是raw中的body请求)都拿不到实体类的值,只有通过
1、加@RequestBody并通过body实体请求

package com.example.test_1.controller;

import com.example.test_1.entity.TestVO;
import org.springframework.web.bind.annotation.*;

@RestController
public class TestGet01 {

    @RequestMapping(value = "/test",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public String test(@RequestBody TestVO testVO){
        System.out.println(testVO);
        return "true";
    }


}

在这里插入图片描述
在这里插入图片描述

2、用@ModelAttribute注解或者不用 post-man中的表单请求:form-data
在这里插入图片描述

package com.example.test_1.controller;

import com.example.test_1.entity.TestVO;
import org.springframework.web.bind.annotation.*;

@RestController
public class TestGet01 {

    @RequestMapping(value = "/test",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public String test(@ModelAttribute TestVO testVO){
        System.out.println(testVO);
        return "true";
    }


}

当使用特殊情况:get请求使用x-www-form形式请求时候

参考:https://www.oschina.net/question/3164861_2244765?sort=default

在这里插入图片描述

使用application/x-www-form-urlencoded 是表单提交, 后台body内容都获取的不到的,因为映射成key ,value了。而设备推送的内容是content是一个不是键值对的字符串, 所以就是为什么可以写sokcetServer的流的形式才能获取到。

当请求参数不是键值对的方式传入时候,或者格式不正确,就不能直接得到:

package com.example.test_1.controller;

import com.example.test_1.entity.TestVO;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@RestController
public class TestGet01 {

    @RequestMapping(value = "/test",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public String test(HttpServletRequest request){

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(
                    new InputStreamReader(request.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String line=null;
        StringBuilder buffer = new StringBuilder();
        while(true){
            try {
                if (!((line = reader.readLine())!=null)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            buffer.append(line);
        }
        System.out.println("buffer str : "+buffer.toString());

        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

//        System.out.println(id);
        String id = request.getParameter("id");
        String name = request.getParameter("name");

        System.out.println(id);
        System.out.println(name);
//        System.out.println(testVO);
        return "true";
    }


}

在这里插入图片描述

在这里插入图片描述
最后看到,取出来的是这样的一个字符串!!!

可以看到,这样传参,相当于直接传递了一串字符串进来,是不能够被解析的!!!!:
在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值