Skills101

Advanced Java & Spring Boot · Lesson 16 of 16 · Updated ·

Spring Boot and Microservices Interview Questions — With Short Answers

Revise these before Java backend interviews. Each answer is short on purpose — say it, then give an example from your project (the Course API counts!).

The Spring Boot track at a glance: Core Java, JDBC, Maven, Spring Core, Spring Boot, REST, JPA, Security, Testing, Microservices and Docker

Spring core and Spring Boot

  1. What is dependency injection? The container creates objects and passes their dependencies in (usually through the constructor), so classes stay loosely coupled and testable.
  2. Bean scopes? singleton (default), prototype, request, session.
  3. @Component vs @Bean? @Component on your own class (found by scanning); @Bean on a factory method in a @Configuration class (for library classes).
  4. Two beans of one type? Use @Primary for the default and @Qualifier("name") to pick one.
  5. What does @SpringBootApplication do? Component scan + auto-configuration + configuration.
  6. What is auto-configuration? Boot creates beans based on the classpath and properties; your own beans or properties override it.
  7. How do you change config per environment? Profiles (application-prod.properties, --spring.profiles.active=prod) and environment variables.
  8. Spring vs Spring Boot? Spring is the framework; Boot adds starters, auto-configuration, an embedded server and production features.

REST

  1. PUT vs PATCH? PUT replaces the resource; PATCH changes some fields.
  2. 401 vs 403? 401 = not authenticated; 403 = authenticated but not allowed.
  3. @Controller vs @RestController? @RestController = @Controller + @ResponseBody (return values become JSON).
  4. @PathVariable vs @RequestParam? /courses/{id} vs /courses?maxFee=3000.
  5. How do you validate input? Bean Validation annotations on the DTO + @Valid; handle errors in @RestControllerAdvice.
  6. Why DTOs instead of entities? Control what's exposed, avoid lazy-loading and recursion issues, keep the API stable.

JPA, Hibernate and transactions

  1. JPA vs Hibernate? JPA is the specification; Hibernate is the most-used implementation.
  2. What is the N+1 problem? One query for a list + one per row for a lazy relationship; fix with JOIN FETCH or entity graphs.
  3. LAZY vs EAGER? LAZY loads on first access; EAGER loads immediately. Prefer LAZY and fetch what you need.
  4. save() vs dirty checking? Managed entities are saved automatically at commit; save() is needed for new entities.
  5. How does @Transactional work? A proxy starts a transaction before the method and commits or rolls back after; rolls back on unchecked exceptions; self-calls bypass it.
  6. Optimistic vs pessimistic locking? @Version column checked at update (conflicts rare) vs locking rows while reading (conflicts frequent).

Security

  1. How does JWT authentication work? Login returns a signed token; clients send Authorization: Bearer …; the server verifies the signature and expiry on every request — no session.
  2. Where do you store passwords? Hashed with bcrypt/argon2, never plain or MD5.
  3. When do you disable CSRF? For stateless token APIs without cookies; keep it for cookie-based web apps.

Testing

  1. @WebMvcTest vs @SpringBootTest? Web slice with mocked services vs the whole application context.
  2. @Mock vs @MockitoBean? @Mock in plain Mockito tests; @MockitoBean replaces a bean inside a Spring test context.

Microservices

  1. Why microservices? Independent deployment, scaling and team ownership — at the cost of network complexity.
  2. Service discovery? Services register by name (Eureka, Kubernetes Services) and callers look them up.
  3. What does an API gateway do? Single entry point: routing, auth, rate limiting, CORS.
  4. Circuit breaker? Stops calling a failing service (OPEN), uses a fallback, tests again after a while (HALF-OPEN).
  5. Synchronous vs asynchronous communication? REST/Feign when you need an answer now; events (Kafka) to notify others and stay decoupled.
  6. How do you keep data consistent across services? Each service owns its data; use events and the Saga pattern; the outbox pattern for reliable publishing.
  7. Kafka consumer group? Instances sharing a topic's partitions; each group receives every event.

Docker

  1. Image vs container? Template vs running instance.
  2. Why multi-stage builds? Build with the JDK, ship only a JRE and the jar.

How to answer well

  • Use the project you built: "In my Course API, I used a join entity for enrolments and JOIN FETCH to avoid N+1."
  • Admit trade-offs: "Microservices add network failures, so I added a timeout, a circuit breaker and a fallback."
  • Put the four example projects on your GitHub with a README, and you have a portfolio piece.

Common mistakes

  • Memorising definitions without an example from your own code.
  • Claiming microservices are always better.
  • Mixing up 401/403, JPA/Hibernate, or @Mock/@MockitoBean.

Quick recap

  • Core: DI, scopes, @Primary/@Qualifier, auto-configuration, profiles.
  • Data: N+1, LAZY, dirty checking, @Transactional rules, locking.
  • Security: JWT flow, bcrypt, CSRF. Testing: slices vs full context.
  • Microservices: discovery, gateway, Feign + circuit breaker, Kafka, Docker.

Practice what you learned

📝 Quiz: Spring Boot Interview Questions5 questions · instant answers & explanations · +10 XP · counts towards your free certificateTake the quiz →