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!
So in a spring boot application, application. properties file is used to write the application-related property into that file. This file contains the different configuration which is required to run the application in a different environment, and each environment will have a different property defined by it.
We can change the port of the Spring Boot in the following ways: By Adding the configuration in the application properties of the Spring Boot project. By Implementing the WebServerFactoryCustomizer interface in the component class. Changing the configuration of VM options.
To see all properties in your Spring Boot application, enable the Actuator endpoint called env . This enables an HTTP endpoint which shows all the properties of your application's environment. You can do this by setting the property management.
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.
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:
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
You can create it manually but the default location of application.properties is here
In the your first journey in spring boot project I recommend you to start with Spring Starter Try this link 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With