Monday, July 29, 2024

Spring MVC

What is the swagger in application?

  • A swagger framework allows developers to create interactive machine and human readable API documentation
  • Swagger is an open source, set of uses, specification and tools for dwelling and describing wasteful API’s documentation

What is the difference between @RestController and @Controller

  • Rest Controller is the Combination of two different annotations.@Controller and @ResponseBody.
  • When we annotate with @RestController We do not need to annotate with @ResponseBody
  • @ResponseBody is required because a REST API Should return the complete HTTP response body.

What are the message converters in spring? What is the default message converter?

  • Spring uses Message converter to transform a type of  data like json to Java objects and vice versa.
  • All message converter, implement HTTPMessageConverter class.
  • Json is the Default converter and string uses Jackson library for this.
  • These converters ensures that request and response are seamlessly transformed, allowing us to work with data naturally.

What are the various media type Annotations in spring? How can we produce or consume or type of a resource from a web service in spring? How can an End point produce a json in Spring?

The various Media type annotations in spring are

  • @Produces
    • It defines the endpoints response type.
    • For example, if end point gives adjacent response, then we can give annotation about the end point as @Produces(“application/json”)
  • @Consumes
    • It defines the MIME media type that is service can consume.
    • If an end point Takes Jason as Request body then @Consumes(“application/json”)

Spring Data

What is JPA?
  • JPA Is also called as Java persistence API
  • It is a standard form oracle to map object to database relations.
  • Provides specification and API for JPA
  • Specification is for the JPA provider and vendors like hibernate, Eclipse link, top link or open JPA.
  • API is for developers.
  • Which create an entity and file persist.xml and DAO.
  • Session factory is replaced with Entity Manager Factory
  • Session becomes Entity Manager.
  • We can perform operation operations like persist, merge, find.
  • It also gives us call back annotations like
    • PerPersist
    • PostPersist
    • PreUpdate
What are the various, advantages and disadvantages of hibernate?
  • Advantages
    • Hibernate is database, independent.
    • Hibernate application developed for one database, for example, My sql can be configured for oracle database. 
    • It provides a layered architecture.
    • Hibernate is an implementation of JPA only.
    • Mapping of domain object to relational databases.
    • Caching Framework.
  • Disadvantages
    • We have to give a separate configuration file.
    • Hard to debug
    • Lots of API to learn.
    • Slower than JDBC.
    • Not suitable against batch processing.
What is association in hibernate?
  • Association is a relationship between two database Tables as entries in our Java classes based on their attributes.
  • It is of four types
    • One to one 
    • One to many
    • Many to one
    • Many to many
  • We use @join column for column reference in main entity.
  • We use @oneToOne(mappedBy=“”) in our referenced entity.
How can we connect more than one databases Or use more than one types of database pools in a spring boot project?
  • Define different data source object beans With respective qualifiers.
  • Define different JDBC template beans with respective qualifiers.
  • Use the implementation respective Datasource along with its Jdbc template to fetch data.
How do we implement Pagination in JPA?
  • Client sends a page size, page number and sorting parameters.
  • We find pageable object and find all method.
    • Pagerequest.of(int page, int page size)
  • We must use JPA repository.
  • In JPA repository Pass this pageable object in findAll method.
What is the difference between JPA repository and CRUD repository?
JpaRepository extends PagingAndSortingRepository that extends CrudRepository.
CrudRepository mainly provides CRUD operations.
PagingAndSortingRepository provide methods to perform pagination and sorting of records.
JpaRepository provides JPA related methods such as flushing the persistence context and deleting of records in batch.
Due to their inheritance nature, JpaRepository will have all the behaviors of CrudRepository and PagingAndSortingRepository. So if you don't need the repository to have the functions provided by JpaRepository and PagingAndSortingRepository , use CrudRepository.

Friday, July 26, 2024

Spring Scheduling Tasks

How can we Schedule Cron Expression in Spring Boot?
  • We can use @Scheduled(cron=“@weekly”) at the top of the method.
  • @EnableScheduleing at to of service.
  • @Schedule(cron=“09** SUN”)
  • Enable scheduling tells the spring that our product has methods which needs to be scheduled.
  • Scheduled annotation takes a  cron expression And executes them As per the cron schedule

Thursday, July 25, 2024

Spring AOP

 What is spring AOP?

  • Spring AOP is aspect oriented programming
  • All layers have some aspects in common like security, profiling, logging, transaction management
  • Focus is on aspects enhancement, their reusability, Cross Cutting concerns
  • Crosscutting concern is a common functionality which is required in entire apps or across the apps.

Monday, July 22, 2024

Situations

  • There is a service, which is providing data with many columns but we require only 10 columns. The service fetches data for many joins. We have the database available with us. We can directly use hibernate calls for getting data which can help in reducing the time complexity.
  • Data Inconsistency is to be handled when one services and other updates, the record
    • We can save the progress data in non-relational database like Mango DB
    • Request, service will broadcast and error to topic in Kafka Which will be consumed by other services to perform specific cleanup.
    • We can also have schedule running that will check for transactions failed, and will retry them and if A transaction is not success, then we retry For specific attempts and then leave it And send response in a log.

Tuesday, July 2, 2024

Spring Boot Docker

How can we scale our application in docker at runtime?
  • we can deploy multiple images of same service.
  • We can use Joule and Dribble to scale microservices.

Spring Message Brokers

 In Kafka, how can we consume and produce messages Between services?

  • Kafka consumer and producer configuration Where we provide the topics which we want to consume from and the topics where we want to produce messages to.
  • In This Kafka configuration producer configuration,We can set the bootstrap URL And key serialiser and value serialiser and Use Kafka template.
  • For listener service We can configure the kafka listener Configuration which consumes messages.

Spring Cloud

What is spring cloud configuration server?

We do not have to keep any configuration file in any of the micro services. All services fetch data from the centralised GitHub repository.

How can we modify an existing spring boot application to convert into a server-less architecture?
  • Break down the spring boot monolithic application into the smaller and self-contained services and containerise using docker.
  • Refactor the code of each of the services To work as independent server less function. Removing unnecessary dependencies.
  • Configure event triggers for these function functions Like defining runtime parameter and deploy them to chosen server less platform.
  • Set up API gateway, if needed.
  • Handle data storage with server less database and ensure proper testing, monitoring and security policies and optimising of cost.
  • Example

Spring Boot

What is circular/cyclic dependency in spring boot? When two services are interdependent on each other, that is to start one service, we requ...