File tree 4 files changed +28
-14
lines changed
spring-webflux/src/main/java/org/springframework/web/reactive/resource
spring-webmvc/src/main/java/org/springframework/web/servlet/resource
4 files changed +28
-14
lines changed Original file line number Diff line number Diff line change @@ -65,20 +65,27 @@ else if (location instanceof ClassPathResource classPathResource) {
65
65
else {
66
66
path = location .getURL ().getPath ();
67
67
}
68
- assertLocationPath (path );
68
+ Assert .isTrue (path .endsWith (FOLDER_SEPARATOR ) || path .endsWith (WINDOWS_FOLDER_SEPARATOR ),
69
+ "Resource location does not end with slash: " + path );
69
70
}
70
71
catch (IOException ex ) {
71
72
// ignore
72
73
}
73
74
}
74
75
75
76
/**
76
- * Assert the given location path is a directory and ends on slash.
77
+ * Check if the given static resource location path ends with a trailing
78
+ * slash, and append it if necessary.
79
+ * @param path the location path
80
+ * @return the resulting path to use
77
81
*/
78
- public static void assertLocationPath (@ Nullable String path ) {
79
- Assert .notNull (path , "Resource location path must not be null" );
80
- Assert .isTrue (path .endsWith (FOLDER_SEPARATOR ) || path .endsWith (WINDOWS_FOLDER_SEPARATOR ),
81
- "Resource location does not end with slash: " + path );
82
+ public static String initLocationPath (String path ) {
83
+ String separator = (path .contains (FOLDER_SEPARATOR ) ? FOLDER_SEPARATOR : WINDOWS_FOLDER_SEPARATOR );
84
+ if (!path .endsWith (separator )) {
85
+ path = path .concat (separator );
86
+ logger .warn ("Appended trailing slash to static resource location: " + path );
87
+ }
88
+ return path ;
82
89
}
83
90
84
91
/**
Original file line number Diff line number Diff line change @@ -379,7 +379,7 @@ private void resolveResourceLocations() {
379
379
Assert .isTrue (CollectionUtils .isEmpty (this .locationResources ), "Please set " +
380
380
"either Resource-based \" locations\" or String-based \" locationValues\" , but not both." );
381
381
for (String location : this .locationValues ) {
382
- ResourceHandlerUtils .assertLocationPath (location );
382
+ location = ResourceHandlerUtils .initLocationPath (location );
383
383
result .add (this .resourceLoader .getResource (location ));
384
384
}
385
385
}
Original file line number Diff line number Diff line change @@ -65,20 +65,27 @@ else if (location instanceof ClassPathResource classPathResource) {
65
65
else {
66
66
path = location .getURL ().getPath ();
67
67
}
68
- assertLocationPath (path );
68
+ Assert .isTrue (path .endsWith (FOLDER_SEPARATOR ) || path .endsWith (WINDOWS_FOLDER_SEPARATOR ),
69
+ "Resource location does not end with slash: " + path );
69
70
}
70
71
catch (IOException ex ) {
71
72
// ignore
72
73
}
73
74
}
74
75
75
76
/**
76
- * Assert the given location path is a directory and ends on slash.
77
+ * Check if the given static resource location path ends with a trailing
78
+ * slash, and append it if necessary.
79
+ * @param path the location path
80
+ * @return the resulting path to use
77
81
*/
78
- public static void assertLocationPath (@ Nullable String path ) {
79
- Assert .notNull (path , "Resource location path must not be null" );
80
- Assert .isTrue (path .endsWith (FOLDER_SEPARATOR ) || path .endsWith (WINDOWS_FOLDER_SEPARATOR ),
81
- "Resource location does not end with slash: " + path );
82
+ public static String initLocationPath (String path ) {
83
+ String separator = (path .contains (FOLDER_SEPARATOR ) ? FOLDER_SEPARATOR : WINDOWS_FOLDER_SEPARATOR );
84
+ if (!path .endsWith (separator )) {
85
+ path = path .concat (separator );
86
+ logger .warn ("Appended trailing slash to static resource location: " + path );
87
+ }
88
+ return path ;
82
89
}
83
90
84
91
/**
Original file line number Diff line number Diff line change @@ -496,7 +496,7 @@ private void resolveResourceLocations() {
496
496
charset = Charset .forName (value );
497
497
location = location .substring (endIndex + 1 );
498
498
}
499
- ResourceHandlerUtils .assertLocationPath (location );
499
+ location = ResourceHandlerUtils .initLocationPath (location );
500
500
Resource resource = applicationContext .getResource (location );
501
501
if (location .equals ("/" ) && !(resource instanceof ServletContextResource )) {
502
502
throw new IllegalStateException (
You can’t perform that action at this time.
0 commit comments