11 Spring Boot
11 Spring Boot
Spring Boot is module of Spring Framework which will speed up the development process.
Spring Boot is nothing but a Spring Framework for Rapid Application Development with extra support of configuration and
embedded server.
Dependency Resolution
Avoid additional configuration
Embedded Tomcat, Jetty
Provide Production ready features such as metrics, health checks
What all Spring Boot Starter you have used or what all modules you have worked on?
Spring Boot Starter Web
Spring Boot Starter Data JPA
Spring Boot Starter AOP
Spring Boot Starter Web Service
Spring Boot Starter Security
Spring Boot Starter Apache Kafka
Spring Boot Starter Spring Cloud
Can we use @Service in repository class and @Controller in Service class and so on?
Yes we can use but it cannot have any logic to do.
Where to use setter injection over constructor injection and vice versa?
Have you worked on Restful web services? If yes what all HTTP methods have you used in your project?
PUT
POST
GET
DELETE
How can you specify the HTTP method type for your REST endpoint?
Can you design a rest endpoint, Assume that you have a product database, and your task is to create an API to filter a list
of products by productType.
Design endpoints in a way that takes "productType" as input. If the user provides this input, the endpoint should filter
products based on the specified condition. If "productType" is not provided, the endpoint should return all the products.
How can we deserialize a JSON request payload into an object within a Spring MVC controller?
Using @RequestBody annotation we are instructing to the spring to deserialize the incoming JSON request to the
particular object
Can we perform update operation in POST http method if yes then why do we need PUT mapping or put http method?
Yes we can do it but its violet the rules of HTTP method.
PostMapping is create the new resource.
PutMapping is use to update the existing resource.
How can you customize the status code for your endpoint?
Just add @ResponseStatus annotation.
@ResponseStatus(HttpStatus.CEATED)
Can you explain deference between YML and properties file and which scenario you might prefer that?
YML is more readable in complex configuration and in structured way.
In properties file it does not follow any hierarchy it just plain line we write it any order.
In YML reduce prefix.
In YML it support list and arrays.
How can you hide certain REST endpoints to prevent them from being exposed externally?
@ControllerAdvice
This annotation is use for to handle all exceptions globally.
It applies globally to all the controller whenever ever any exception occur.
@ControllerAdvice
This annotation handle the specific Exception and send custome responses back to client.
How can you avoid defining handlers for multiple exceptions, or what is the best practise for handling exceptions?
use case: lets say you find a bug in production environment and now you want to debug the scenario, how can you do
that from your local?
How can you enable a specific environment without using profiles? OR what is the alternative to profiles to achieving
same use case?
We will add property in application.propertis file
Spring.profiles.active = prod
What is AOP?
We can do using AOP each and every secondary logic we can define as an aspect where we can write this transaction
related or logging, validation or notification and so on which secondary logic and we can tell to spring here we can use this
kind of logic will use.
Aspect is nothing a methodology or a class who contains the secondary logic.
use case - Can I use AOP of evaluate performance of a method or is this possible to design a logging framework to capture
request and response body of a method?
How does your application interact with the database and which framework are you using?
Add dependency
Add important data source properties in application.properties file.
In this file we have Dialect – dialect will help JPA to generate a query behalf of us.
Create POJO class and add some annotations.
Now create repository interface and extends JPA repository.
What are the differences between hibernate JPA and Spring Data JPA ?
How can you connect multiple databases or data sources in a single application ?
What are the different ways to define custom queries in spring Data JPA ?
How will you define entity relationships or association mapping in spring Data JPA ?
Is this possible to execute join query in spring data JPA ? if yes, how can you add some insights ?
How will you implement pagination and sorting in Spring Data JPA ?
If same configuration write on both YML and Properties file then which value load first?
Properties file will load first.
@Configuartion
@ConfigurationProperties(prefix = “spring.datasource”)
@Data
@AllArgsConstructor
@Component
public class ConfigProprties
{
private String username;
private String password;
}
Can you explain when we should use singleton scope and prototype scope?
Singleton
Database Configuration, Service layer, Application Configuration
Prototype
User Session, Thread Safe, Heavy Initialization
What is the purpose of the BeanPostProcessor interface in Spring, and how can you use it to customize bean
initialization and destruction?
What all Spring Boot starters you have used or what all modules you have worked on?