Skip to content

Commit ae861a2

Browse files
committed
Document leading slash semantics for ClassPathResource.getPath()
See gh-29094
1 parent 0395fb4 commit ae861a2

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

spring-core/src/main/java/org/springframework/core/io/ClassPathResource.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,25 @@ else if (absolutePath.startsWith("/")) {
121121
/**
122122
* Return the path for this resource.
123123
* <p>If this resource was created using
124-
* {@link ClassPathResource#ClassPathResource(String) ClassPathResource(String)},
125-
* {@link ClassPathResource#ClassPathResource(String, ClassLoader)
126-
* ClassPathResource(String, ClassLoader)}, or
124+
* {@link ClassPathResource#ClassPathResource(String) ClassPathResource(String)}
125+
* or {@link ClassPathResource#ClassPathResource(String, ClassLoader)
126+
* ClassPathResource(String, ClassLoader)}, the returned path is a
127+
* {@linkplain StringUtils#cleanPath(String) cleaned} version of the
128+
* <em>absolute path</em> supplied to the constructor, <strong>without</strong>
129+
* a leading slash.
130+
* <p>If this resource was created using
127131
* {@link ClassPathResource#ClassPathResource(String, Class)
128132
* ClassPathResource(String, Class)} with an absolute path, the returned path
129133
* is a {@linkplain StringUtils#cleanPath(String) cleaned} version of the
130-
* <em>absolute path</em> supplied to the constructor.
134+
* <em>absolute path</em> supplied to the constructor, <strong>with</strong>
135+
* a leading slash.
131136
* <p>If this resource was created using
132137
* {@link ClassPathResource#ClassPathResource(String, Class)
133138
* ClassPathResource(String, Class)} with a relative path, the returned path
134139
* is a {@linkplain StringUtils#cleanPath(String) cleaned} version of the
135140
* <em>relative path</em> supplied to the constructor.
141+
* <p>The path returned by this method cannot be reliably used with
142+
* {@link ClassLoader#getResource(String)}.
136143
* <p>If you consistently need the <em>absolute path</em>, use
137144
* {@link #getAbsolutePath()} instead.
138145
* @see #getAbsolutePath()
@@ -143,7 +150,9 @@ public final String getPath() {
143150

144151
/**
145152
* Return the <em>absolute path</em> for this resource, as a resource path
146-
* within the class path.
153+
* within the class path without a leading slash.
154+
* <p>The path returned by this method is suitable for use with
155+
* {@link ClassLoader#getResource(String)}.
147156
* @since 6.0
148157
* @see #getPath()
149158
*/

0 commit comments

Comments
 (0)