Skip to content

Commit 7615e0b

Browse files
committed
Un-deprecate PathResource (for NIO Path resolution in createRelative)
Includes aligned createRelative signature and dedicated java.io.File test. Closes gh-24211
1 parent afe22b8 commit 7615e0b

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,6 +49,7 @@
4949
*
5050
* @author Juergen Hoeller
5151
* @since 28.12.2003
52+
* @see #FileSystemResource(String)
5253
* @see #FileSystemResource(File)
5354
* @see #FileSystemResource(Path)
5455
* @see java.io.File
@@ -108,6 +109,15 @@ public FileSystemResource(File file) {
108109
* <p>In contrast to {@link PathResource}, this variant strictly follows the
109110
* general {@link FileSystemResource} conventions, in particular in terms of
110111
* path cleaning and {@link #createRelative(String)} handling.
112+
* <p>Note: When building relative resources via {@link #createRelative},
113+
* the relative path will apply <i>at the same directory level</i>:
114+
* e.g. Paths.get("C:/dir1"), relative path "dir2" -> "C:/dir2"!
115+
* If you prefer to have relative paths built underneath the given root directory,
116+
* use the {@link #FileSystemResource(String) constructor with a file path}
117+
* to append a trailing slash to the root path: "C:/dir1/", which indicates
118+
* this directory as root for all relative paths. Alternatively, consider
119+
* using {@link PathResource#PathResource(Path)} for {@code java.nio.path.Path}
120+
* resolution in {@code createRelative}, always nesting relative paths.
111121
* @param filePath a Path handle to a file
112122
* @since 5.1
113123
* @see #FileSystemResource(File)

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,15 +44,16 @@
4444
* in {@link FileSystemResource#FileSystemResource(Path) FileSystemResource},
4545
* applying Spring's standard String-based path transformations but
4646
* performing all operations via the {@link java.nio.file.Files} API.
47+
* This {@code PathResource} is effectively a pure {@code java.nio.path.Path}
48+
* based alternative with different {@code createRelative} behavior.
4749
*
4850
* @author Philippe Marschall
4951
* @author Juergen Hoeller
5052
* @since 4.0
5153
* @see java.nio.file.Path
5254
* @see java.nio.file.Files
53-
* @deprecated as of 5.1.1, in favor of {@link FileSystemResource#FileSystemResource(Path)}
55+
* @see FileSystemResource
5456
*/
55-
@Deprecated
5657
public class PathResource extends AbstractResource implements WritableResource {
5758

5859
private final Path path;
@@ -252,7 +253,7 @@ public long lastModified() throws IOException {
252253
* @see java.nio.file.Path#resolve(String)
253254
*/
254255
@Override
255-
public Resource createRelative(String relativePath) throws IOException {
256+
public Resource createRelative(String relativePath) {
256257
return new PathResource(this.path.resolve(relativePath));
257258
}
258259

spring-core/src/test/java/org/springframework/core/io/ResourceTests.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.core.io;
1818

1919
import java.io.ByteArrayInputStream;
20+
import java.io.File;
2021
import java.io.FileNotFoundException;
2122
import java.io.IOException;
2223
import java.io.InputStream;
@@ -129,6 +130,14 @@ public void testFileSystemResource() throws IOException {
129130
assertEquals(new FileSystemResource(file), resource);
130131
}
131132

133+
@Test
134+
public void fileSystemResourceWithFile() throws IOException {
135+
File file = new File(getClass().getResource("Resource.class").getFile());
136+
Resource resource = new FileSystemResource(file);
137+
doTestResource(resource);
138+
assertEquals(new FileSystemResource(file), resource);
139+
}
140+
132141
@Test
133142
public void testFileSystemResourceWithFilePath() throws Exception {
134143
Path filePath = Paths.get(getClass().getResource("Resource.class").toURI());

0 commit comments

Comments
 (0)