1. How do you create a RESTful web service in Spring Boot? Provide a simple example.
You create a RESTful web service by annotating a class with @RestController and defining handler methods. Here’s a simple example:
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, World!";
}
}
2. What is the purpose of the @RestController annotation, and how does it differ from @Controller?
@RestController combines @Controller and @ResponseBody, making it suitable for RESTful APIs. It automatically serializes return values to JSON/XML.
3. How can you handle exceptions in a Spring Boot application?
You can use @ControllerAdvice and @ExceptionHandler to create global exception handling for your application or use specific exception handlers within controllers.
4. What is Spring Boot Actuator, and what kind of information can it provide about your application?
Spring Boot Actuator provides production-ready features like health checks, metrics, and application-specific endpoints for monitoring and managing your application.
5. Explain the differences between Spring Boot’s embedded web servers: Tomcat, Jetty, and Undertow.
Spring Boot allows you to choose between these embedded web servers. Tomcat is widely used, Jetty is known for its low overhead, and Undertow is designed for high performance.
6. What is Spring Boot DevTools, and how can it help in the development process?
DevTools is a set of tools that enhances development by providing automatic application restarts, hot swapping, and improved error reporting during development.
7. How do you implement security in a Spring Boot application? Mention some security features and mechanisms.
You can implement security using Spring Security, which provides authentication, authorization, and features like JWT-based authentication and OAuth 2.0 support.
8. What is Spring Boot’s support for microservices architecture? How can you create microservices using Spring Boot?
Spring Boot supports microservices by allowing you to create independent, self-contained services. Each microservice can be a separate Spring Boot application, communicating via REST or messaging.
9. What are Spring Boot Profiles, and how are they used for different environments?
Profiles allow you to define environment-specific configurations. You can activate profiles using the spring.profiles.active property in application.properties or application.yml.
10. Can you explain the differences between Spring Boot and Spring Cloud?
Spring Boot simplifies the development of standalone applications, while Spring Cloud provides tools for building and deploying microservices-based, distributed systems, including features like service discovery and distributed configuration.