Spring Boot & REST API Interview Questions (Beginner to Intermediate)

What is Spring Boot?

Answer:

Spring Boot is a framework built on top of Spring that simplifies the development of Java applications by reducing configuration and setup time.

Key Features:

  • Auto-configuration
  • Embedded server (Tomcat)
  • Production-ready features
  • Minimal configuration

Why use Spring Boot?

Answer:

Spring Boot is used to quickly build stand-alone and production-ready applications with less boilerplate code.

Benefits:

  • Faster development
  • Easy deployment
  • No need for XML configuration
  • Microservices friendly

What is a REST API?

Answer:

REST (Representational State Transfer) API is a web service that allows communication between client and server using HTTP methods.

HTTP Methods:

  • GET → Fetch data
  • POST → Create data
  • PUT → Update data
  • DELETE → Remove data

What is @RestController?

Answer:

@RestController is a combination of @Controller and @ResponseBody. It is used to create RESTful web services.

Example:

“`java id=”r1″
@RestController
@RequestMapping(“/api”)
public class MyController {

@GetMapping("/hello")
public String hello() {
    return "Hello World";
}

}

---

## What is @RequestMapping?

###  Answer:

@RequestMapping is used to map HTTP requests to handler methods in a controller.

### Example:

java id=”r2″
@RequestMapping(value = “/test”, method = RequestMethod.GET)
public String test() {
return “Test API”;
}

---

## What is @Autowired?

###  Answer:

@Autowired is used for dependency injection. It automatically injects required beans.

---

## What is Dependency Injection?

### Answer:

Dependency Injection is a design pattern where objects are provided to a class instead of creating them inside the class.

---

## What is Spring Boot Starter?

### Answer:

Spring Boot starters are pre-configured dependencies that simplify project setup.

### Example:

* spring-boot-starter-web
* spring-boot-starter-data-jpa

---

##  What is Exception Handling in Spring Boot?

### Answer:

Spring Boot provides @ControllerAdvice and @ExceptionHandler to handle exceptions globally.

### Example:

java id=”r3″
@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(Exception.class)
public String handleException() {
    return "Error occurred";
}

}

---

##  What is JWT (JSON Web Token)?

### Answer:

JWT is a token-based authentication mechanism used to securely transmit information between client and server.

---

## JWT Flow:

1. User logs in
2. Server generates JWT token
3. Client stores token
4. Client sends token in header
5. Server validates token

---

## JWT Example (Header)

java id=”r4″
Authorization: Bearer
“`


Advantages of JWT:

  • Stateless authentication
  • Secure
  • Scalable for microservices

Interview Tip:

Always explain with:
✔ Definition
✔ Real-world use
✔ Code example

Leave a Comment