007SpringBootInterviewQuestions PDF
007SpringBootInterviewQuestions PDF
Here's how you can implement content negotiation in a Spring Boot REST API:
3. **Using ContentNegotiationManager**:
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer
configurer) {
configurer
.defaultContentType(MediaType.APPLICATION_JSON)
.mediaType("json", MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML);
}
}
4. **Using RequestMappingHandlerMapping**:
java
@RestController
@RequestMapping("/api")
public class MyController {
@Autowired
private RequestMappingHandlerMapping requestMappingHandlerMapping;
@GetMapping(value = "/resource")
public ResponseEntity<Resource> getResource(HttpServletRequest request)
throws Exception {
HandlerMethod handlerMethod = (HandlerMethod)
requestMappingHandlerMapping.getHandler(request).getHandler();
produces =
handlerMethod.getMethodAnnotation(RequestMapping.class).produces();
if (produces.contains(MediaType.APPLICATION_JSON_VALUE)) {
// Retrieve resource as JSON
} else if (produces.contains(MediaType.APPLICATION_XML_VALUE)) {
// Retrieve resource as XML
}
}
}
What is the purpose of Spring Boot's @Value annotation for accessing application
properties?
How do you implement content negotiation in a Spring Boot REST API for different
media types?
Describe the use of Spring Boot @RequestMapping and @GetMapping annotations for
URL mapping.
How do you configure Spring Boot to send email notifications using JavaMail or
SendGrid?
To configure Spring Boot to send email notifications using JavaMail or SendGrid,
you need to set up the necessary dependencies, configure the mail server
properties, and create a service or component to handle the email sending logic.
Here's a step-by-step guide:
1. **Add Dependencies**:
If you're using JavaMail, include the following dependencies in your
`pom.xml`:
```xml
<dependency>
<groupId>org.springframework.boot//groupId>
<artifactId>spring-boot-starter-mail//artifactId>
//dependency>
```
If you're using SendGrid, include the SendGrid dependency along with Spring
Boot Mail starter:
```xml
<dependency>
<groupId>org.springframework.boot//groupId>
<artifactId>spring-boot-starter-mail//artifactId>
//dependency>
<dependency>
<groupId>com.sendgrid//groupId>
<artifactId>sendgrid-java//artifactId>
<version>${sendgrid.version}//version>
//dependency>
```
@Service
public class EmailService {
@Autowired
private JavaMailSender javaMailSender;
That's it! Your Spring Boot application should now be configured to send email
notifications using either JavaMail or SendGrid. Make sure to handle exceptions
appropriately, especially when dealing with messaging exceptions in the email
service.
Explain the differences between Spring Boot's actuator endpoints like /health and
/info.
/actuator/auditevents
Exposes audit events information for the current application.
/actuator/beans
Displays a complete list of all the Spring beans in your application.
/actuator/caches
Exposes available caches.
/actuator/conditions
Shows the conditions that were evaluated on configuration and auto-configuration
classes and the reasons why they did or did not match.
/actuator/configprops
Displays a collated list of all @ConfigurationProperties.
/actuator/env
Exposes properties from Spring’s ConfigurableEnvironment.
/actuator/flyway
Shows any Flyway database migrations that have been applied.
/actuator/health
Shows application health information.
/actuator/httptrace
Displays HTTP trace information (by default, the last 100 HTTP request-response
exchanges).
/actuator/info
Displays arbitrary application info.
/actuator/integrationgraph
Shows the Spring Integration graph.
/actuator/loggers
Shows and modifies the configuration of loggers in the application.
/actuator/liquibase
Shows any Liquibase database migrations that have been applied.
/actuator/metrics
Shows ‘metrics’ information for the current application.
/actuator/mappings
Displays a collated list of all @RequestMapping paths.
/actuator/scheduledtasks
Displays the scheduled tasks in your application.
/actuator/sessions
Allows retrieval and deletion of user sessions from a Spring Session-backed
session store. Not available when using Spring Session’s support for reactive web
applications.
/actuator/shutdown
Let the application be gracefully shutdown.
/actuator/threaddump
Performs a thread dump.
Describe Spring Boot's support for reactive programming with Project Reactor.
Spring Boot provides extensive support for reactive programming with Project
Reactor, which is a reactive library for building non-blocking, asynchronous
applications on the JVM. Here's how Spring Boot leverages Project Reactor for
reactive programming:
1. WebFlux Framework: Spring Boot includes the WebFlux framework, which is a
reactive web framework built on top of Project Reactor. WebFlux provides
support for building reactive, non-blocking web applications using annotated
controllers and functional endpoints.
2. Reactive WebClient: Spring Boot includes a reactive WebClient that
integrates with Project Reactor. The WebClient allows you to make
non-blocking HTTP requests to external services and APIs. It supports
various HTTP clients, including WebClient.Builder and WebClient.create, for
making GET, POST, PUT, DELETE, and other types of requests.
3. Reactive Data Access: Spring Boot offers reactive support for data access
through Spring Data Reactive repositories. With reactive repositories, you
can perform asynchronous database operations using Project Reactor's Mono
and Flux types. Spring Data provides support for reactive database drivers,
such as R2DBC for relational databases and Spring Data MongoDB for MongoDB.
4. Reactive Configuration and Auto-Configuration: Spring Boot provides
auto-configuration support for reactive components, such as WebClient,
WebClient.Builder, and reactive data sources. It automatically configures
these components based on the dependencies and configuration properties in
the application context.