๐ฑ Spring Boot Interview Series – Q5 ๐ก: Server Ports, HTTPS, Profiles & Hidden Tricks ๐๐๐ ️
๐ Changing the Default Server Port
By default, Spring Boot runs your application on port 8080
. You can change this by adding the following in your application.properties
:
server.port=9090
Or in application.yml
:
server:
port: 9090
server.port=0
to make Spring Boot choose a random free port — useful for integration tests.@Bean
methods like TomcatServletWebServerFactory
but forgetting to remove server.port
from application.properties
can cause confusion. Properties file wins unless overridden by command-line args.๐ Enabling HTTPS in Spring Boot
To enable HTTPS, you’ll need a keystore file (e.g., keystore.p12
) and then configure Spring Boot:
server.port=8443
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=changeit
server.ssl.keyStoreType=PKCS12
server.ssl.keyAlias=tomcat
TomcatConnectorCustomizer
bean for port 8080
while keeping 8443
as HTTPS.๐ซ Disabling the Web Server
If you’re creating a non-web application (like a batch job or CLI tool), you can disable the embedded server:
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MyApp.class);
app.setWebApplicationType(WebApplicationType.NONE);
app.run(args);
}
}
Or via properties:
spring.main.web-application-type=none
๐ฆ Default Packaging Type in Spring Boot
When you create a Spring Boot project using Spring Initializr, the default packaging type is JAR. This means the embedded server (Tomcat, by default) is inside your packaged app, so you can run it with:
java -jar myapp.jar
<packaging>war</packaging>
in Maven.SpringBootServletInitializer
will cause the app not to start on an external server.๐ application.properties vs application.yml
Both are used for externalizing configuration, but:
- application.properties: Key-value format, simpler, familiar for many developers.
- application.yml: YAML format, supports hierarchy, cleaner for nested configs.
application.properties
overrides application.yml
for the same property..yml
— YAML does not support tabs, only spaces, causing ScannerException
.๐งฉ How Profiles Work in Spring Boot
Profiles let you group configurations for different environments (dev, test, prod). You can create files like:
application-dev.properties
application-prod.properties
Activate a profile via:
spring.profiles.active=dev
spring.profiles.active=dev,qa
.๐ฌ Wrapping Up
Wow! Today we explored some of Spring Boot’s hidden gems — from changing server ports ๐ฅ️, enabling HTTPS ๐, disabling the web server ๐ซ, understanding default packaging ๐ฆ, comparing application.properties
vs application.yml
๐️, to managing profiles ๐ฑ.
๐ก Here’s what you should take away:
- Spring Boot makes life easier, but small misconfigurations (wrong port, YAML indentation, wrong profile) can cause big headaches ๐
- Always double-check your
spring.profiles.active
and SSL setups before deploying to production ⚠️ - Use embedded servers wisely — or disable them for non-web apps to save resources ๐ช
- Know when to use JAR vs WAR packaging depending on deployment targets ๐️
๐ฅ My challenge to you: Try these configs in a small test project and see how changing one thing can affect the entire app. It’s a fun way to learn & remember! ๐
๐ฌ I’d love to hear from you: Which Spring Boot trick surprised you the most? Drop your thoughts in the comments below ๐ — let’s learn together!
๐ Hashtags: #SpringBoot #JavaDeveloper #InterviewPrep #JavaLearning #SpringTips #Microservices #CodingLife #TechBlog
Comments
Post a Comment