I'm trying to make an app's REST API more RESTful and it feels like I'm not using the Spring RequestMappings in the way intended.
I have a single GET end point for doing reads:
@RequestMapping(value = "thing/{thingName}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String getThing(
@PathVariable(value = "thingName", required = false)
String thingName,
@RequestParam(value = "findByComponent", required = false)
String findByComponentQuery,
@AuthenticationPrincipal User user) {
...
To be more restful, I want this endpoint to do both:
- GET /thing/{thingName} returns a single thing having that name
- GET /thing or /thing/ with query params returns lists of things
So in my controller, I can test whether {thingName}
is null or zero-length and if so, check the query params for known query names.
However calling this with http://localhost:8080/thing/?findByComponent=component123
returns a 404 from Spring with this logging:
12:45:18.485 PageNotFound : No mapping found for HTTP request with URI [/thing/] in DispatcherServlet with name 'dispatcher' : WARN : XNIO-1 task-3 : org.springframework.web.servlet.DispatcherServlet