Spring MockMvc错误匹配控制器处理器方法问题求助
Hey there, let's figure out why your MockMvc tests are misfiring when trying to match your controller methods. Based on the code you shared, here are the most common culprits and fixes to check:
1. Double-check your request method in tests
Your two controller methods use distinct HTTP verbs:
findByUuidrelies on GETfindByStringPropertyEqualsuses POST
If you accidentally use the wrong verb in your MockMvc test (e.g., calling .get() for the POST endpoint), Spring won't be able to match the right method. Make sure your test aligns with the controller's configured verb:
- For the GET endpoint: use
.get() - For the POST endpoint: use
.post()
Example correct test snippet for the GET method:
mockMvc.perform(get("/actual-uuid-value") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk());
2. Verify path matching accuracy
- For the GET endpoint
/\{uuid\}: You must replace the{uuid}placeholder with a real value in your test. Using the literal/{uuid}won't trigger the path variable match. - For the POST endpoint
/findByStringPropertyEquals: Ensure the path in your test is an exact match—no typos, missing slashes, or extra URL segments.
3. Don't skip required request parameters
Your POST method has three mandatory @RequestParam parameters: type, property, and value. If your test omits any of these, Spring won't find a matching method (it'll scan for methods that accept fewer parameters, which don't exist here).
Include all three parameters in your POST test:
mockMvc.perform(post("/findByStringPropertyEquals") .param("type", "sample-type") .param("property", "sample-property") .param("value", "sample-value") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk());
4. Match the produces media type
Both methods specify produces = "application/json", meaning they only handle requests where the client accepts JSON. If your test doesn't set the Accept header to application/json, Spring might skip these methods entirely.
Always include .accept(MediaType.APPLICATION_JSON) in your MockMvc requests (like the examples above) to align with the controller's produces setting.
5. Check for class-level @RequestMapping
If your controller class has a top-level @RequestMapping (e.g., @RequestMapping("/api/myobjects")), you need to append that base path to your test URLs. For example, the GET endpoint would become /api/myobjects/actual-uuid-value instead of just /actual-uuid-value.
6. Enable debug logging for deeper insight
If none of the above fixes work, turn on DEBUG logging for org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping. This will show you exactly which controller methods Spring is evaluating for your request, and why it's rejecting them (e.g., missing parameters, mismatched HTTP method, etc.).
To enable this, add this line to your logback.xml (or equivalent logging config):
<logger name="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" level="DEBUG"/>
内容的提问来源于stack exchange,提问作者Steffen Harbich

