SpringMVc @RequestMapping url of regular expression pattern

@RequestMapping(method = RequestMethod.GET,value="/{provinceId}_{levelId}.htm")
    public Map<String, Appointment> test() {
        return appointmentBook.getAppointmentsForToday();
    }
@RequestMapping(method = RequestMethod.GET,value="/{provinceId}_{levelId}_s{id}.htm")
    public Map<String, Appointment> test1() {
        return appointmentBook.getAppointmentsForToday();
    }

  As with the above two URLs, when I enter /2_3_s1.htm on the browser (here omitting the thing in front of the URL), I always enter the first method, and the order of your two methods is also the same, always enter the first method. One, the problem is not difficult to see. In fact, the first URL contains the first one. Both methods of the request link of /2_3_s1.htm are consistent (equivalent to treating 3_s1 as a whole, so the first method is also consistent). As for why it entered the first One, it should be that the first URL contains the second, which is equivalent to a parent-child relationship.
Solution:

Change the first method to the following

1
2
3
4
@RequestMapping(method = RequestMethod.GET,value="/{provinceId}_{levelId:\\d*}.htm")
    public Map<String, Appointment> test() {
        return appointmentBook.getAppointmentsForToday();
    }

  It is to add \\d* after levelId, which means that levelId can only match integers. If written in this way, our previous link/2_3_s1.htm will enter the second method, because you treat 3_s1 even as A link does not meet the condition of an integer, so it will enter the second method.
\\d{6}: Represents 6 digits
\\?-[0,9]d: Represents a negative integer
[az]{3}: Three letters,
etc.

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *