Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

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

I involved in project where I found a mix of:

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

(with and without slash before method level annotation).

I perform search:

site:http://static.springsource.org/spring/docs/3.1.x  slash

and read these links:

But none of these sources answer why skipping slash allowed. Official Spring docs always shown examples with slashes...

Need point to official docs or to Spring sources.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
    }   

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...