如何在Spring Boot控制器中检索查询参数? [英] How do I retrieve query parameters in a Spring Boot controller?

查看:56
本文介绍了如何在Spring Boot控制器中检索查询参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot开发一个项目.我有一个接受 GET 请求的控制器.

I am developing a project using Spring Boot. I've a controller which accepts GET requests.

当前,我正在接受对以下类型URL的请求:

Currently I'm accepting requests to the following kind of URLs:

http://localhost:8888/user/data/002

但我想接受使用查询参数的请求:

but I want to accept requests using query parameters:

http://localhost:8888/user?data = 002

这是我的控制器的代码:

Here's the code of my controller:

@RequestMapping(value="/data/{itemid}", method = RequestMethod.GET)
public @ResponseBody
item getitem(@PathVariable("itemid") String itemid) {   
    item i = itemDao.findOne(itemid);              
    String itemname = i.getItemname();
    String price = i.getPrice();
    return i;
}

推荐答案

使用 @RequestParam

@RequestMapping(value="user", method = RequestMethod.GET)
public @ResponseBody Item getItem(@RequestParam("data") String itemid){

    Item i = itemDao.findOne(itemid);              
    String itemName = i.getItemName();
    String price = i.getPrice();
    return i;
}

这篇关于如何在Spring Boot控制器中检索查询参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆