Spring 的 MockMvc 选择了错误的处理程序方法
Spring's MockMvc picks up wrong handler method
我有一个包含 2 个以上方法的控制器:
@RequestMapping(method = RequestMethod.GET, value = "/{uuid}", produces = "application/json")
public MyObject findByUuid(
@PathVariable("uuid") String uuid) {
...
}
@RequestMapping(method = RequestMethod.POST, value = "/findByStringPropertyEquals", produces = "application/json")
public List<MyObject> findByStringPropertyEquals(
@RequestParam("type") String type,
@RequestParam("property") String property,
@RequestParam("value") String value) {
...
}
现在当我尝试测试第二种方法时
mvc.perform(get("/findByStringPropertyEquals?type={0}&property={1}&value={2}", "Person", "testPropInt", 42))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(1)));
然后 MockMvc
不幸地选择了 findByUuid
控制器方法。输出是
MockHttpServletRequest:
HTTP Method = GET
Request URI = /api/v1/domain-entities/findByStringPropertyEquals
Parameters = {type=[Person], property=[testPropInt], value=[42]}
Headers = {}
Body = <no character encoding set>
Session Attrs = {}
Handler:
Type = com.example.MyObjectController
Method = public com.example.MyObject com.example.MyObjectController.findByUuid(java.lang.String)
但是,在 Web 服务器启动时定期访问 REST API 工作正常。 这是错误还是我做错了什么?
在您的代码中,您正在调用 GET:
mvc.perform(get("/findByStringPropertyEquals?type={0}&property={1}&value={2}", "Person", "testPropInt", 42))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(1)));
然而,findByStringPropertyEquals
方法是为 POST 声明的,因此您的代码无法使用 GET 寻址该方法。我不确定为什么 MockMVC 层选择了 findByUuid
(可能是因为这是该控制器上唯一的 GET 方法)但是您的代码没有达到 findByStringPropertyEquals
的原因是您选择了错误的 HTTP方法。
试试 mvc.perform(post(...))
。
我有一个包含 2 个以上方法的控制器:
@RequestMapping(method = RequestMethod.GET, value = "/{uuid}", produces = "application/json")
public MyObject findByUuid(
@PathVariable("uuid") String uuid) {
...
}
@RequestMapping(method = RequestMethod.POST, value = "/findByStringPropertyEquals", produces = "application/json")
public List<MyObject> findByStringPropertyEquals(
@RequestParam("type") String type,
@RequestParam("property") String property,
@RequestParam("value") String value) {
...
}
现在当我尝试测试第二种方法时
mvc.perform(get("/findByStringPropertyEquals?type={0}&property={1}&value={2}", "Person", "testPropInt", 42))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(1)));
然后 MockMvc
不幸地选择了 findByUuid
控制器方法。输出是
MockHttpServletRequest:
HTTP Method = GET
Request URI = /api/v1/domain-entities/findByStringPropertyEquals
Parameters = {type=[Person], property=[testPropInt], value=[42]}
Headers = {}
Body = <no character encoding set>
Session Attrs = {}
Handler:
Type = com.example.MyObjectController
Method = public com.example.MyObject com.example.MyObjectController.findByUuid(java.lang.String)
但是,在 Web 服务器启动时定期访问 REST API 工作正常。 这是错误还是我做错了什么?
在您的代码中,您正在调用 GET:
mvc.perform(get("/findByStringPropertyEquals?type={0}&property={1}&value={2}", "Person", "testPropInt", 42))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(1)));
然而,findByStringPropertyEquals
方法是为 POST 声明的,因此您的代码无法使用 GET 寻址该方法。我不确定为什么 MockMVC 层选择了 findByUuid
(可能是因为这是该控制器上唯一的 GET 方法)但是您的代码没有达到 findByStringPropertyEquals
的原因是您选择了错误的 HTTP方法。
试试 mvc.perform(post(...))
。