如何获取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';

如果我事先知道参数(例如“名字”),那么我可以这样做...

@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现在是所有参数及其传递的键值映射。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...