Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

可以用@RequestBody接收GET请求吗?

SpringBoot后端,axios前端发请求,但是不论把参数放在params还是data里,请求返回的都是400 Bad Request.

上网查了一些资料,看到说@GetMapping@RequestBody是互斥的,但是我用postman却能成功传请求拿到返回值.

后端代码

    @GetMapping(value = "/view/tagtree")
    public SingleNodeConsanguinity viewTagTree(@RequestBody TagLocateInfoBean tagLocateInfoBean, HttpServletRequest request) {
        SingleNodeConsanguinity singleNodeConsanguinity =  viewTagService.getConsanguinity(tagLocateInfoBean);
        return singleNodeConsanguinity;
    }

前端代码

let param = new URLSearchParams();
            param.append("type", "QFX");
            param.append("sheetName", "获取数据");
            param.append("tag", "");
            param.append("bqId", "");
            console.log(param);
            axios.get("http://localhost:8080/qfx/demand/view/tagtree",
                param
            )
                .then((res) => {
                    console.log(res);
                })
                .catch(err => {
                    console.log('出错了');
                    console.log(err);
                });

postman

图片.png
图片.png

浏览器

图片.png

问问题想知道

  • @RequestBody能用来接收get请求的参数吗?
  • 如果能,那应该怎么操作才能成功把请求发到后端,拿到数据呢?
  • 如果不能, 那为什么postman能发送成功呢?

提前谢谢大佬


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

涨知识了,Get请求居然支持Body。
说下关于问题我的理解吧。
1.支持,个人建议非必要不要这样用。
2.你请求失败应该是 axios不支持get传递body(我是后端这个回答来自于https://www.zhihu.com/questio...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...