Use or not leading slash in value for @RequestMapping. Need official docs or point to Spring source?

后端 未结 1 1531
故里飘歌
故里飘歌 2020-12-06 03:57

I involved in project where I found a mix of:

@RequestMapping(value = \"events/...\");
@RequestMapping(value = \"/events/...\");

(with and without slas

相关标签:
1条回答
  • 2020-12-06 04:43

    It does not matter: If the path does not start with an / then Spring (DefaultAnnotationHandlerMapping) will add it.

    See method String[] determineUrlsForHandler(String beanName) of Class DefaultAnnotationHandlerMapping line 122 (Spring 3.1.2) (that is for the class level)

    String[] methodLevelPatterns = determineUrlsForHandlerMethods(handlerType, true);
    for (String typeLevelPattern : typeLevelPatterns) {
        if (!typeLevelPattern.startsWith("/")) {
                typeLevelPattern = "/" + typeLevelPattern;
        }
    

    See method String[] determineUrlsForHandler(Class<?> handlerType, final boolean hasTypeLevelMapping)) of Class DefaultAnnotationHandlerMapping line 182 (Spring 3.1.2) (that is for the method level)

    String[] mappedPatterns = mapping.value();
    if (mappedPatterns.length > 0) {
    for (String mappedPattern : mappedPatterns) {
        if (!hasTypeLevelMapping && !mappedPattern.startsWith("/")) {
            mappedPattern = "/" + mappedPattern;
        }   
    
    0 讨论(0)
提交回复
热议问题