弹簧不允许路径变量(
{thingName}
)映射到空
String
. 实际上,这意味着
/thing/?findByComponent=component123
做
不
映射到
thing/{thingName}
空着
{TngNe}
但是,它希望有一些映射
thing
. 因为没有映射到路径的终结点
事情
(不带路径变量),a
404
返回错误。
要解决此问题,可以将此单端点拆分为两个单独的端点:
@RequestMapping(value = "thing/{thingName}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String getThing(
@PathVariable(value = "thingName") String thingName,
@AuthenticationPrincipal User user) {
}
@RequestMapping(value = "thing",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String getThings(,
@RequestParam(value = "findByComponent", required = false) String findByComponentQuery,
@AuthenticationPrincipal User user) {
}
有关详细信息,请参阅
With Spring 3.0, can I make an optional path variable?
.
这个
required=false
标志允许两种类型的请求:
-
/thing
-
/thing/<some_value>
严格来说,包括URL结尾的尾随斜杠(即
/thing/
)表示已决定包含路径变量的值,但未提供任何值。在RESTAPI的上下文中,
物
和
/事物/
是两个不同的端点,后者意味着期望尾随斜杠后面的值。
不必创建三个单独的请求映射(上面的每种情况都有一个)的解决方法是设置
@RequestMapping
控制器到基路径的值,然后具有
""
和
"/{thingName}
两个端点的请求映射:
@RestController
@RequestMapping("thing")
public class ThingController {
@RequestMapping(value = "/{thingName}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String getThing(
@PathVariable(value = "thingName") String thingName) {
return "foo";
}
@RequestMapping(value = "",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String getThings(
@RequestParam(value = "findByComponent", required = false) String findByComponentQuery) {
return "bar";
}
}
在这种情况下,将发生以下映射:
-
物
:
getThings
-
/事物/
:
获得事物
-
/thing/foo
:
getThing
这个解决方法的一个例子,包括测试用例可以
found here
.