我正在尝试为我的应用程序开发新的Web服务。
为此,我使用Spring REST-webservice
。
在控制器端,我试图根据传递的代理获取记录列表。现在需要,代理可以传递或者可以为null。如果是null代理,则应选择所有记录。仅获取那些记录被拿走了。
尝试使用下面的代码来实现动态。,根据搜索结果之一,它不起作用。
@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:当我使用像。的URL时,
www.example.com/myapplication/batches/staging/1234
它工作正常,并取得了所需的结果。
情况2:但是如果我没有传递任何参数说。,
www.example.com/myapplication/batches/staging/
在哪里,我没有传递任何参数。,它说我找不到映射。
你能告诉我如何在REST
GET请求方法调用中实现这个动态URL。
提前致谢!!
您可以使用@Pathvariable
作为URL中的可选值,而不是使用@RequestParam
。
所以你的网址就像。
案例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
}
使用@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;
}