问题描述
我有一个Spring REST API,我不知道参数名会提前。就像这样...
/myapp/api/employees?firstname=Bob&lastname=Jones
基本上变成... SELECT * FROM employees WHERE firstname = 'bob' and lastname = 'jones';
/myapp/api/customers?customerNumber=12345
基本上变成... SELECT * FROM customers WHERE customerNumber = '12345';
如果我事先知道参数(例如“名字”),那么我可以这样做...
@RequestMapping(value = "/{entityType}",method = RequestMethod.GET,produces = "application/json")
public ResponseEntity<String> getEntity(@PathVariable String entityType,@RequestParam(required = false) String firstname) throws Exception {
...但是我不知道这些参数的名称。他们可以是任何东西。
解决方法
回答我自己的问题。在this article here中找到了解决方案...
@RequestMapping(value = "/{entityType}",method = RequestMethod.GET,produces = "application/json")
public ResponseEntity<String> getEntity(@PathVariable String entityType,@RequestParam Map<String,String> allParams) throws Exception {
allParams
现在是所有参数及其传递的键值映射。