如何获取 Spring REST URL 的请求参数的名称?
How do I get the names of the request parameters for a Spring REST URL?
我有一个 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';
如果我事先知道参数(比如 'firstname'),那么我可以这样做...
@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
现在是传递给它的所有参数和值的 key-value 映射。
我有一个 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';
如果我事先知道参数(比如 'firstname'),那么我可以这样做...
@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
现在是传递给它的所有参数和值的 key-value 映射。