如何在url(如/api/groups)中获取参数?sdk&;使用java时type=1?
例如,URL可以是:如何在url(如/api/groups)中获取参数?sdk&;使用java时type=1?,java,spring-mvc,restful-url,Java,Spring Mvc,Restful Url,例如,URL可以是: /api/groups?sdk&type=1 或 在java中,我想知道url中的参数是sdk或app。 我试过这样的方法: @RequestMapping(method = RequestMethod.GET) public ResponseResult testGet(HttpServletRequest request, @RequestParam String sdk, @RequestParam int type) { ... } 您可以为ap
/api/groups?sdk&type=1
或
在java中,我想知道url中的参数是sdk或app。
我试过这样的方法:
@RequestMapping(method = RequestMethod.GET)
public ResponseResult testGet(HttpServletRequest request, @RequestParam String sdk, @RequestParam int type) {
...
}
您可以为
app
和sdk
使用参数,因此您的url将是/api/groups?param=sdk&type=1
或/api/groups?param=app&type=1
。您可以在下面找到示例代码:
@RequestMapping(value = "/groups", method = RequestMethod.GET)
public RestResponse testGet(@RequestParam(value = "param", required = false) String param, @RequestParam(value = "type", required = false) String type) {
}
基本上,您可以有两种方法,并使用
@RequestMapping
操作中的params
变量来区分这两种方法
@RequestMapping(method = RequestMethod.GET, params="sdk")
public ResponseResult getSdk(HttpServletRequest request, @RequestParam int type) {
...
}
@RequestMapping(method = RequestMethod.GET, params="app")
public ResponseResult getApp(HttpServletRequest request, @RequestParam int type) {
...
}
您可能需要也可能不需要将
value=“/groups”
添加到请求映射中,具体取决于您配置类/应用程序的方式。使用@PathVariable为什么不将其设置为另一个查询参数?所以它可能是这样的:/api/groups?param=app
你能举个例子吗?@shantaram\u tCan我想问一下你想要实现什么?是否需要一个请求处理程序方法来处理这两个问题?或者,您是否希望为每个参数设置一个方法?这将定义您的实现。我想知道url是sdk还是应用程序中的参数,并且我想知道每个url的方法parameter@MartinByers
@RequestMapping(method = RequestMethod.GET, params="sdk")
public ResponseResult getSdk(HttpServletRequest request, @RequestParam int type) {
...
}
@RequestMapping(method = RequestMethod.GET, params="app")
public ResponseResult getApp(HttpServletRequest request, @RequestParam int type) {
...
}