问题描述
我正在尝试为我的应用程序开发新的Web服务。
为此,我正在使用Spring REST-webservice
。
在控制器端,我试图根据传递的代理获取记录列表。现在需求可以传递代理,也可以为空。如果代理为空,则应选择所有记录,否则仅选择那些记录待取。
根据搜索结果之一,尝试使用下面的代码来实现动态。但是,它不起作用。
@RequestMapping(value = "/staging/{agentCode: [^/]*?}" , method =
RequestMethod.GET)
这是我现有的代码:
@Controller
@RequestMapping(value="/batches")
public class BatchController {
@SuppressWarnings({ "rawtypes" })
@RequestMapping(value="/staging/{agentCode}", method =
RequestMethod.GET)
@ResponseBody
public ResponseEntity IntmBatch(@PathVariable("agentCode") String
agentCode)
{
//code here
}
情况1 :当我使用类似。的网址时,
www.example.com/myapplication/batches/staging/1234
它工作正常,并且获取了所需的结果。
情况2 :但是,如果我没有传递任何参数,
www.example.com/myapplication/batches/staging/
在中,我没有传递任何参数。,它表示找不到映射。
您能否让我知道如何在REST
GET请求方法调用中实现此动态URL。
提前致谢!!
1楼
可以使用@RequestParam
代替URL的可选值, @Pathvariable
使用@Pathvariable
。
因此,您的网址将是这样。
案例1 : www.example.com/myapplication/batches/staging?agentCode=1234
&
案例2 : www.example.com/myapplication/batches/staging
希望它能解决您的问题。
@SuppressWarnings({ "rawtypes" })
@RequestMapping(value="/staging", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity IntmBatch(@RequestParam(name="agentCode",required=false) String agentCode)
{
//code here
}
2楼
在控制器中使用@RequestMapping(value =“ / staging”,method = RequestMethod.GET)创建另一个方法,如下所示。
@RequestMapping(value = "/staging", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity returnAll() {
System.out.println("returning all ");
// code here
return null;
}