Search code examples
javaspring-bootget-mapping

GetMapping in Spring boot to exclude specific path variable match


I have a controller with the following GetMapping

@GetMapping(value = "/dawson/v1/{dataType}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<String> respondWithData(@PathVariable String dataType, @RequestParam(name = "minDate") String min_date, @RequestParam(name = "maxDate") String max_date, @RequestParam(USERID) String user_id, @RequestHeader(value = "Authorization") String authorizationHeader) {

where dataType can be one of either String, Map, Object or Calendar. I created another GetMapping as follows

@GetMapping(value = "/dawson/v1/signInReq")
public ResponseEntity<String> mySignInRequest(@RequestBody Map<String, String> paramMap, HttpServletRequest request, HttpServletResponse response) {

However, when I try to access the /dawson/v1/signInReq, it still hits the first mapping and not the signInReq one. Is there a way to exclude signInReq as a match for {dataType}?

I do have the workaround of listing all possible dataTypes in the first mapping above but was wondering if there was a better way to handle it(Regex maybe?).


Solution

  • I used a REGEX pattern to resolve the filtering of path as follows:

    @GetMapping(value = "/dawson/v1/{dataType:^.*(?!signInReq)}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    

    The Regex will only match requests that are not signInReq.