用逗号分隔的 @PathVariables 映射的 Rest 端点
Rest endpoint mapped with @PathVariables separated by comma
我有 @RestController
和 @RequestMapping("/api/my-resource")
。我有 @GetMapping
用于提取特定实体。
@GetMapping(value = "/{firstId},{secondId}")
public ResponseEntity<MyResourceDTO> findMyResource(
@PathVariable Long firstId, @PathVariable String secondId) {
//here I have firstId == null and secondId == null
}
如果我用 /
替换 ,
一切正常,但要求是不要使用另一个 /
。
我可以确认,我可以进入这个方法,但是两个 @PathVariable
都映射为空。 Spring
支持这种映射吗?我做错了什么?
我想实现类似于this的东西,但我必须使用Spring
。
编辑:
我看过 List
的解决方案,但我不想使用它,因为 ID 的类型不同,所以它不是重复的。我正在使用 <spring.version>4.3.6.RELEASE</spring.version>
给定一个带有此声明的控制器...
@GetMapping(value = "/this/that/find/{firstId},{secondId}")
public ResponseEntity<String> findMyResource(@PathVariable int firstId, @PathVariable String secondId) {
return new ResponseEntity<>(""+ firstId + "-" + secondId, HttpStatus.OK);
}
...此测试通过:
@Test
public void testWithCommaDelimitedPathVariables() throws Exception {
MvcResult mvcResult = mockMvc.perform(get("/this/that/find/1,a"))
.andExpect(status().isOk())
.andReturn();
Assert.assertEquals("1-a", mvcResult.getResponse().getContentAsString());
}
因此,Spring 支持声明 "Rest endpoint mapped with @PathVariables separated by comma"。
使用Spring 4.3.10.RELEASE
使用您当前的 Spring 版本,您可以尝试使用 _
作为分隔符,它应该可以正常工作。
我在 Spring 文档中读过一次,但现在找不到 link,我会尽快找到它。
我有 @RestController
和 @RequestMapping("/api/my-resource")
。我有 @GetMapping
用于提取特定实体。
@GetMapping(value = "/{firstId},{secondId}")
public ResponseEntity<MyResourceDTO> findMyResource(
@PathVariable Long firstId, @PathVariable String secondId) {
//here I have firstId == null and secondId == null
}
如果我用 /
替换 ,
一切正常,但要求是不要使用另一个 /
。
我可以确认,我可以进入这个方法,但是两个 @PathVariable
都映射为空。 Spring
支持这种映射吗?我做错了什么?
我想实现类似于this的东西,但我必须使用Spring
。
编辑:
我看过 List
的解决方案,但我不想使用它,因为 ID 的类型不同,所以它不是重复的。我正在使用 <spring.version>4.3.6.RELEASE</spring.version>
给定一个带有此声明的控制器...
@GetMapping(value = "/this/that/find/{firstId},{secondId}")
public ResponseEntity<String> findMyResource(@PathVariable int firstId, @PathVariable String secondId) {
return new ResponseEntity<>(""+ firstId + "-" + secondId, HttpStatus.OK);
}
...此测试通过:
@Test
public void testWithCommaDelimitedPathVariables() throws Exception {
MvcResult mvcResult = mockMvc.perform(get("/this/that/find/1,a"))
.andExpect(status().isOk())
.andReturn();
Assert.assertEquals("1-a", mvcResult.getResponse().getContentAsString());
}
因此,Spring 支持声明 "Rest endpoint mapped with @PathVariables separated by comma"。
使用Spring 4.3.10.RELEASE
使用您当前的 Spring 版本,您可以尝试使用 _
作为分隔符,它应该可以正常工作。
我在 Spring 文档中读过一次,但现在找不到 link,我会尽快找到它。