Improve resource handling for empty files contained in jars #28850
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We noticed a strange behavior in WebMVC when working with empty files:
We had some empty files in a classpath location configured in
org.springframework.web.servlet.config.annotation.WebMvcConfigurer#addResourceHandlers
When running the application locally using the IDE, the files could be "successfully" accessed by the browser (it got a HTTP 200 response with content-length 0)
After packaging the application, the browser suddenly got 404 responses for the empty files.
The difference seems to be in
org.springframework.core.io.AbstractFileResolvingResource#checkReadable
.As long as the empty files are actual files on the file system (
ResourceUtils.isFileURL(url) == true
), the corresponding ClasspathResource is considered both existent and readable.After packaging into a jar file, the ClasspathResource is still existent, but no longer considered readable.
I know that serving empty files as static resources is not a typical use case, but I think it should still be (a) possible and (b) consistent, regardless of whether the ClasspathResource in question is an actual File or a JarFileEntry