I've created a Demo REST API using Spring Boot Framework in Java. I wanted to deploy this API on Tomcat 8 as a WAR file. The API is already configured to compile down as a WAR file using Spring Initializr. But, when the API is deployed, its not reading application.properties
file from the project.
@PropertySource
and @PropertySources
annotations.<resources>
tag in <build>
tag in pom.xml file..properties("classpath:application.properties")
to ServletInitializer
Nothing worked
I've used context-path as an example in this post. In my actual application, I have some properties stored in application.properties and the application is not reading them.
Here's my code files:
server.servlet.context-path=/api
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
@RestController
@RequestMapping("/user")
public class User {
@GetMapping("")
public String getUser() {
return "User is here...";
}
}
Hitting localhost:8080/demo/api/user
URL should give our desired output, but its rather giving a 404 result. Whereas, hitting localhost:8080/demo/user
is giving the desired output.
This means that the applicaiton is not reading application.properties file because the default route of the api was set to /api
in that file.
Only embedded tomcat honors the server.servlet.context-path
. If you are deploying to an external tomcat, follow the guidelines here to set the context path: https://tomcat.apache.org/tomcat-8.0-doc/config/context.html
Now, verify whether application.properties
is bundled in the WAR
file. To do this extract or open the WAR
file. If not included then verify your build file whether resource plugin is copying the resource files.
If the application.properties
is included in the WAR then, try reading other properties in your application.