Home > Software engineering >  How to give url path in spring boot refer to local file
How to give url path in spring boot refer to local file

Time:07-23

I am trying to consume the WSDL file in the spring boot application, WSDL file is placed in the src/main/resource/wsdl/file.wsdl project structure

Java code

     static {
        URL url = null;
        try {
            
            url = new URL("file:///"   System.getProperty("user.dir")   "/src/main/resources/wsdl/outbound.wsdl");
        
        } catch (IOException e) {
            java.util.logging.Logger.getLogger(OutboundService.class.getName())
                .log(java.util.logging.Level.INFO,
                     "Can not initialize the default wsdl from {0}", "file:/D:/ERPLOGIC/ERPProjects/JAVA/mavenproject/eclipse-workspace/topconpoc/src/main/resources/wsdl/outbound.wsdl");
        }
        WSDL_LOCATION = url;
    }

it works fine in locally, but when deployed in the AWS server as a war file, it does not work, What is the proper to refer to the path in the spring boot application.

CodePudding user response:

The file is placed inside the Java resources directory src/main/resource and should be loaded from there in all cases. The reason it's not loading properly when deployed is that file is not in that location once packaged. Use the class to locate the resource file from the packaged Java app. This should work both locally and when deployed. Make sure to replace ClassFileWhereCodeLives with the class the static code block is inside of.

url = ClassFileWhereCodeLives.class.getResource("/wsdl/outbound.wsdl")

CodePudding user response:

Try this

URL url = Thread.currentThread().getContextClassLoader().getResource("wsdl/outbound.wsdl");

This should work

  • Related