87

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?

1
  • You should create file manually if no exists manually
    – Pau
    Commented Aug 5, 2016 at 4:12

5 Answers 5

91

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.

3
  • First I want to thank for your answear. I tried this, but it does not work for me. Tomcat even starts with a random port.
    – Mira Mira
    Commented Aug 4, 2016 at 22:00
  • I started a new project with java spring boot. And here it works.
    – Mira Mira
    Commented Aug 5, 2016 at 7:55
  • If you want to add directly to a WAR, just place it under /WEB-INF/classes/ Commented Jul 22, 2019 at 13:31
28

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

enter image description here

1
  • Yes even I agreed that this(src/main/resources/app.yml) is the default location for application.yml (or application.properties). But recently I am working on Kotlin with SPringboot and I discovered (while working on Junits) that its expecting the app.yml to be present at "test/resources/config/application.yml" and NOT at "test/resources/application.yml" - when did this change come ? Have I missed something ? Commented Oct 25, 2022 at 6:59
25

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

5

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
1
  • How to access let's say from any class the port variable?
    – user13963099
    Commented Mar 5, 2021 at 15:40
0

application.properties file is present in src/main/resources/application.properties if it is not present you can create your own file

Example

server.port=9091
student.postgres.db=postgresdburl

How to consume - student.postgres.db -> value in your code

@Value("${student.postgres.db}")
private String jdbcUrl;

I hope this can solve your query let me know if still you are not able to access the property file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.