Skip to main content

🌱 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

πŸ’‘ Tip: You can set server.port=0 to make Spring Boot choose a random free port — useful for integration tests.
Common Mistake: Changing the port in @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

πŸ’‘ Unknown Fact: Spring Boot can run HTTP and HTTPS together — just define another TomcatConnectorCustomizer bean for port 8080 while keeping 8443 as HTTPS.
Common Mistake: Using a self-signed certificate in production without adding it to the truststore will break API calls from browsers and other services.

🚫 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
πŸ’‘ Tip: This reduces startup time and memory usage since Tomcat/Jetty/Undertow is not loaded.
Common Mistake: Developers forget to remove web-related beans/controllers when disabling the server — Spring will still try to load them and fail.

πŸ“¦ 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
πŸ’‘ Extra Info: You can switch to WAR if you want to deploy in external servers like Apache Tomcat — just change <packaging>war</packaging> in Maven.
Common Mistake: Switching to WAR but forgetting to extend 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.
πŸ’‘ Extra Info: Spring Boot loads both by default — if both exist, application.properties overrides application.yml for the same property.
Common Mistake: Using tabs in .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
πŸ’‘ Unknown Fact: You can activate multiple profiles at once using spring.profiles.active=dev,qa.
Common Mistake: Forgetting to set the active profile in production — your app might accidentally run with dev settings (like an in-memory DB).

🎬 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

Popular posts from this blog

🐱 Tomcat vs ⚡ Netty – Which One Should You Use?

🐱 Tomcat vs ⚡ Netty – Which One Should You Use? So recently I got curious about this too πŸ€”. Everywhere in Spring Boot tutorials we see Tomcat . Then suddenly while exploring Spring WebFlux , the name Netty pops up. And I was like – “Wait, who’s this Netty guy trying to replace Tomcat?” πŸ˜… Let’s break it down with real-time examples , icons , and fun comparisons . 🐱 Tomcat – The Traditional Web Server Type: Servlet Container (blocking I/O) World: Used with Spring MVC Style: Thread-per-request model πŸ‘©‍πŸ’» Pros: Stable, widely used, battle-tested Cons: Struggles with huge concurrent connections πŸ‘‰ Example in real life: Tomcat is like a restaurant with fixed waiters 🍴. - Each customer = one thread/waiter - If too many customers come in at once → waiters run out → customers wait outside πŸšͺ ⚡ Netty – The Reactive Rockstar Type: Asynchronous Event-Driven Network Framework World: Default for Spring WebFlux Style: Event-lo...

🎭 Spring’s Secret: Why @Transactional & Friends Betray You Silently

πŸ’‘ Lesson Learned — Not a Prod Bug, But a Real Pain No, this wasn’t a production outage. Nobody screamed at me. But I sat for 3 hours wondering: “Why the heck is my @Transactional not rolling back!?” 😡‍πŸ’« “Why is Redis cache not working?” 🀯 Turned out, the issue was one silent villain: 🧱 Self-invocation 🀷 What Is @Transactional ? If you're new: @Transactional = Tells Spring to start a DB transaction when a method is called. It’ll commit if everything’s okay. It’ll rollback if something fails. 🧠 Think of it like wrapping your code in: try { beginTransaction(); // your logic commit(); } catch(Exception e) { rollback(); } πŸ•΅️ Real-Life Analogy — The Gateway Community 🏘️ Let me tell you about my society — it has a strict watchman at the gate. Here’s how it works: πŸ›‚ Watchman = Spring Proxy 🏠 Your apartment = Your service class πŸšͺ Your room = A method inside that class πŸƒ Scenario 1: Outsider Visits Your friend from outside...

🌟 My Journey – From Zero to Senior Java Tech Lead 🌟

 There’s one thing I truly believe… If I can become a Java developer, then anyone in the world can. πŸ’― Sounds crazy? Let me take you back. πŸ•“ Back in 2015… I had zero coding knowledge . Not just that — I had no interest in coding either. But life has its own plans. In 2016, I got a chance to move to Bangalore and joined a Java course at a training center. That’s where it all started — Every day, every session made me feel like: "Ohhh! Even I can be a developer!" That course didn’t just teach Java — it gave me confidence . πŸ§ͺ Two Life-Changing Incidents 1️⃣ The Interview That Wasn't Planned Halfway through my course, I had to urgently travel to Chennai to donate blood to a family member. After that emotional rollercoaster, I found myself reflecting on my skills and the future. The next day, as I was preparing for my move to Bangalore to complete the remaining four months of my course, I randomly thought — "Let me test my skills... let me just see...