Where is the application.properties file in a Spring Boot project?

JavaSpringSpring Boot

Java Problem Overview


I started a new Spring boot project, I want to change the port number and I read that I have to modify the /resource/application.properties to do so.

I cannot locate this file however, did I miss something? Do I need to install a boot starter? I don't want to set this using the spring CLI.

Should I create this file manually? If so, I think I'll have to mark this file as the properties file somewhere in the code. Where would that be?

Thanks a lot!

Java Solutions


Solution 1 - Java

You will need to add the application.properties file in your classpath.

If you are using Maven or Gradle, you can just put the file under src/main/resources.
If you are not using Maven or any other build tools, put that under your src folder and you should be fine.

Then you can just add an entry server.port = xxxx in the properties file.

Solution 2 - Java

You can create it manually but the default location of application.properties is here

enter image description here

Solution 3 - Java

You can also create the application.properties file manually.

SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:

  • A /config subdirectory of the current directory.
  • The current directory
  • A classpath /config package
  • The classpath root

The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations). (From the Spring boot features external configuration doc page)

So just go ahead and create it

Solution 4 - Java

In the your first journey in spring boot project I recommend you to start with Spring Starter Try this link here.

enter image description here

It will auto generate the project structure for you like this.application.perperties it will be under /resources.

application.properties important change,

server.port = Your PORT(XXXX) by default=8080
server.servlet.context-path=/api (SpringBoot version 2.x.)
server.contextPath-path=/api (SpringBoot version < 2.x.)

Any way you can use application.yml in case you don't want to make redundancy properties setting.

Example
application.yml

server:
   port: 8080 
   contextPath: /api

application.properties

server.port = 8080
server.contextPath = /api

Solution 5 - Java

Spring Boot will automatically find and load application.properties and application.yaml files from the following locations when your application starts:

  1. The classpath root
  2. The classpath /config package
  3. The current directory
  4. The /config subdirectory in the current directory
  5. Immediate child directories of the /config subdirectory

The list is ordered by precedence (with values from lower items overriding earlier ones).

More info you can find here https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-files

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMira MiraView Question on Stackoverflow
Solution 1 - JavaMinjun YuView Answer on Stackoverflow
Solution 2 - JavaAchillesVanView Answer on Stackoverflow
Solution 3 - JavaalainlompoView Answer on Stackoverflow
Solution 4 - JavasoypheaView Answer on Stackoverflow
Solution 5 - JavaSabfirView Answer on Stackoverflow