Spring Boot Interview Questions and Answers
Spring Boot interviews reward candidates who can say why the framework does something, not just which annotation to type. These are the questions that come up, answered from the mechanism outwards.
- 24questions
- 5basic
- 12intermediate
- 7advanced
Core Spring
6 questionsDependency injection and the container. Everything else builds on this.
Constructor injection or field injection?
final so the object cannot exist half-built, they are visible in the signature so a class with too many is obviously doing too much, and the class can be constructed in a plain unit test with no Spring at all. Field injection hides all three.What does @SpringBootApplication actually do?
@Configuration marks the class as a source of bean definitions, @ComponentScan scans this package and below for components, and @EnableAutoConfiguration switches on Boot's conditional configuration.How does auto-configuration work?
@ConditionalOnClass, @ConditionalOnMissingBean, @ConditionalOnProperty. At startup it evaluates them: a database driver on the classpath and no DataSource of your own means Boot defines one. Defining your own bean makes the condition fail, so your definition wins.--debug for the auto-configuration report, which lists every condition that matched and every one that did not, with the reason.What are the bean scopes, and which is the default?
What is the difference between @Component, @Service, @Repository and @Controller?
@Components that say what the class is for. @Repository additionally translates persistence exceptions into Spring's DataAccessException hierarchy, and @Controller/@RestController are what web request mapping looks for. So the difference is intent, plus that one real behaviour.REST and web
5 questionsThe half of the interview that maps to what you actually built.
What is the difference between @Controller and @RestController?
@RestController is @Controller plus @ResponseBody on every method, so return values are serialised into the response body instead of being resolved as view names. Use it for APIs; use @Controller when you are rendering server-side templates.How do you handle exceptions across a whole API?
@RestControllerAdvice class with @ExceptionHandler methods. It centralises the mapping from exception to status code and response body, so controllers stay free of try/catch and every error in the API comes back in the same shape — which is what clients need.@RestControllerAdvice
class ApiExceptionHandler {
@ExceptionHandler(NotFoundException.class)
ResponseEntity<ApiError> notFound(NotFoundException e) {
return ResponseEntity.status(404).body(new ApiError(e.getMessage()));
}
}What is the difference between @RequestParam, @PathVariable and @RequestBody?
@PathVariable binds part of the URL path and is for identifying a resource. @RequestParam binds a query parameter and is for filtering, sorting and paging. @RequestBody deserialises the request body, for the payload of a POST or PUT.Which HTTP status codes should an API return, and when?
Location header when something was created, 204 when there is nothing to return. 400 for a malformed or invalid request, 401 not authenticated, 403 authenticated but not allowed, 404 not found, 409 for a conflict such as a duplicate. 500 only for an unexpected server failure — a validation error returning 500 is a common review comment.How do you validate a request body?
@NotBlank, @Email, @Min — and put @Valid on the controller parameter. A failure raises MethodArgumentNotValidException, which you map in the advice class above so clients get one consistent error shape with the field names in it.Data and transactions
5 questionsWhere the questions get specific and the wrong answer is visible.
What does @Transactional actually do?
RuntimeException only — a checked exception commits unless you set rollbackFor. Second, because it works through a proxy, calling a @Transactional method from another method in the same class bypasses the proxy entirely and no transaction starts.What is the N+1 query problem?
JOIN FETCH in the query, an entity graph, or a batch size so the n queries become a handful.What is the difference between JPA, Hibernate and Spring Data JPA?
findByEmailAndActiveTrue needs no body.What is the difference between lazy and eager loading?
LazyInitializationException when the association is touched after the session closed, which is a sign the fetch should have been part of the query.How do you manage configuration across environments?
application-dev.yml, application-prod.yml) activated by a profile, with @ConfigurationProperties binding them to typed objects rather than scattering @Value strings. Secrets come from environment variables or a secret manager, never from a file in the repository.Testing, security and services
8 questionsThe last third of the interview, and where projects get discussed.
What is the difference between @SpringBootTest and @WebMvcTest?
@SpringBootTest starts the whole application context — thorough and slow, right for an integration test. @WebMvcTest loads only the web layer with the rest mocked, so a controller test runs in a fraction of the time. Loading the full context for every test is the usual reason a suite becomes too slow to run.How do you test a service class?
What is the difference between @Mock and @MockBean?
@Mock is Mockito's and creates a mock object with no Spring involved. @MockBean puts a mock into the Spring context, replacing the real bean — which also invalidates the cached context, so overusing it slows the suite by rebuilding the context repeatedly.How does authentication and authorisation work in Spring Security?
Authentication is placed in the SecurityContext. Authorisation then decides whether that principal may do this, through URL rules in the chain or @PreAuthorize on methods. Authentication is identity; authorisation is permission.How would you secure a REST API with JWT?
How do you call another service from Spring Boot?
RestClient or WebClient in current versions; RestTemplate is the older, maintenance-mode option. Whatever you use, set a connect and read timeout — an unbounded call to a hanging dependency exhausts your thread pool and takes your service down with theirs.What are actuator endpoints?
/health, /metrics, /info, /env — used by load balancers, orchestrators and monitoring. They expose real internals, so expose only what is needed and put them behind authentication or a separate port.What is the difference between a monolith and microservices?
Reading answers is not the same as giving them
Most candidates know the material and still stumble when asked out loud. Take a Spring Boot mock interview where the interviewer follows up on what you actually say.
Other interview question sets
Java
Java interview questions with real answers
JavaScript
JavaScript interview questions with real answers
Python
Python interview questions with real answers
SQL
SQL interview questions with real answers and queries
React
React interview questions with real answers
DSA
DSA interview questions with real answers
System Design
System design interview questions with real answers