1

Spring MVC 请求映射搜索参数上最接近的匹配。今天我们刚刚遇到了一个很好的例子,为什么这个当前的实现是有问题的。我们有两个功能:

@RequestMapping(method = Array[RequestMethod](RequestMethod.DELETE), params = Array[String]("lastName", "firstName"), produces = Array[String]("application/json"))
def deletePersons1(request: HttpServletRequest, @RequestParam("lastName") lastName: String, @RequestParam("firstName") acref: String)
@RequestMapping(method = Array[RequestMethod](RequestMethod.DELETE), params = Array[String]("lastName", "birthDate"), produces = Array[String]("application/json"))
def deletePersons2(request: HttpServletRequest, @RequestParam("lastName") lastName: String, @RequestParam("birthDate") birthDate: Date)

http请求是:

DELETE http://host:port/deletePersons?lastName=smith&firstName=john&birthDate=08-10-2015

用户只想删除 Smith,John 并且还认为他们可以添加出生日期。但是由于第一个函数没有得到日期并且用户犯了一个错误并在那里输入了一个日期,所以在我们的例子中,使用了第二个函数,因为它最接近匹配。我仍然不知道为什么是第二个而不是第一个。

结果是所有姓史密斯、出生在……的人都被删除了。

这是一个真正的问题!因为我们只想删除一个特定的人,但最终删除了许多其他人。

有什么解决办法吗?

4

1 回答 1

1

更新:

问题来自这样一个事实,即您的函数之间存在重叠变量,并且用户试图混合使用它们。为确保此特定问题不会再次发生,您可以明确声明您不想接受包含某些额外变量的请求(当不需要该参数时)。例如,上面的示例问题可以通过更改第二个定义来解决(注意 !firstName 参数)

@RequestMapping(method = Array[RequestMethod](RequestMethod.DELETE), params = Array[String]("lastName", "birthDate"), produces = Array[String]("application/json"))
def deletePersons2(request: HttpServletRequest, @RequestParam("lastName") lastName: String, @RequestParam("birthDate") birthDate: Date)

至:

@RequestMapping(method = Array[RequestMethod](RequestMethod.DELETE), params = Array[String]("!firstName", "lastName", "birthDate"), produces = Array[String]("application/json"))
def deletePersons2(request: HttpServletRequest, @RequestParam("lastName") lastName: String, @RequestParam("birthDate") birthDate: Date)
于 2015-09-07T18:06:54.490 回答