简体   繁体   中英

Spring Boot, property of type Path read from application.yaml

I hope somebody will be able to help me with the following issue I find in Spring Boot version 2.3.5.

I have simple test application (cannot share sources) where:

  • I have a class annotated with @ConfigurationProperties(prefix = "app") and one field private Path path .
  • application.yaml file has configured value path: C:\\\\Local

During application startup, the value of the property configured in application.yaml should print to console. So I have the following behaviour:

  • When I run the application from IntelliJ (Windows machine) it is running properly
  • When I build the fat jar (with Maven Spring Boot plugin) and run the application, it fails with
Property: app.path
Value: C:\\Local
Origin: class path resource [application.yaml]:2:9
Reason: No converter found capable of converting from type [java.lang.String] to type [java.nio.file.Path]
  • When the same jar is run under Linux, having configured Linux path, it is working properly.

Any ideas why so? Or having a reference to a documentation related to the subject?

Edit Class where property print happen:

@SpringBootApplication
public class ApplicationBootstrap implements ApplicationRunner {

    @Autowired private ApplicationProperties properties;

    @Override public void run(final ApplicationArguments args) {
        System.out.println(properties.getPath());
    }

    public static void main(String[] args) {
        SpringApplication.run(ApplicationBootstrap.class);
    }
}

Update So looks like the problem is in Classpath Loader.

The issue appear in org.springframework.beans.propertyeditors.PathEditor on line 100.

else if (!resource.exists() && nioPathCandidate)

When running under IntellJ, the ClassLoaders$AppClassLoader is used, this one can resolve the value C:\\\\Local .

While running from jar, the LaunchedURLClassLoader is used and this fails when executing line 100 with same value, the exception message java.lang.IllegalArgumentException: name

So most probably the issue is in Spring Boot Class Loader.

Update

Issue appear in method findResource() from LaunchedURLClassLoader which delegate to same method to URLClassLoader while the AppClassLoader (which used when running from IntellJ) delegates to same method from BuiltinClassLoader .

Final Answer The problem is that at some point it considers c: as protocol and it fails with an exception.

So the proper configuration in properties file should be: app.path=file:C:\\\\Local

Related issues: https://github.com/spring-projects/spring-boot/issues/7161

Thank you.

First of all your extension of application file should be "yml". Can you please provide your application.yml properties? The following example works for me:

@Component
@ConfigurationProperties(prefix = "app")
public class TestConfig {
  private Path path;

  public Path getPath() {
    return path;
  }

  public void setPath(Path path) {
    this.path = path;
  }
}

And applicaion.yml

app:
  path: C:\FOO\BAR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM