Spring and HTTP Flashcards
What is the difference between Spring and Spring boot?
Spring is an open-source lightweight framework widely used to develop enterprise applications. Spring Boot is built on top of the conventional spring framework, widely used to develop REST APIs.
Is spring boot a backend?
Spring Boot is a backend framework that has become a major player in the enterprise Java ecosystem. It lets Java developers start building web applications quickly, without fuss.
What is Spring?
Spring is essentially an open-source lightweight, integrated framework that can be used for developing enterprise applications in Java.
Spring aims to simplify the complex and cumbersome enterprise Java application development process by offering a framework that includes technologies such as: - > Aspect-oriented programing (AOP), Dependency injections (DI), Plain Old Java Object (POJO), etc.
What are the different modules/features of the Spring framework?
Some of the important Spring Framework modules are:
§ Spring Context - for dependency injection
§ Spring AOP - for aspect oriented programming
§ Spring DAO - for database operations using DAO pattern
§ Spring JDBC - for JDBC and DataSource support
§ Spring ORM - for ORM (Object Relational Mapping) tools support such as Hibernate
§ Spring Web Module - for creating web applications
Spring MVC - Model-View-Controller implementation for creating web applications, web services etc.
List some of the important annotations in annotation-based Spring configuration
@Required - marks a method as being required
@Qualifier - marks a qualifier
@Resource - marks a resource needed by the applications
@PostConstruct - what happens just after the object is constructed
@PreDestroy - what happens before the object is finalized
@Configuration - indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.
@Scope - to set the scope of the bean
@Autowired - dependency injection @Controller - indicates that a particular class serves the role of a controller @Service - is used to mark the class as a service provider. So overall @Service annotation is used with classes that provide some business functionalities. @Repository - indicates that the decorated class is a repository. A repository is a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects. @PathVariable - indicates that a method parameter should be bound to a URI template variable. @RequestMapping - maps HTTP requests to handler methods of MVC and REST controllers. @ResponseBody - binds a method return value to the web response body
What is the difference between @PathVariable and @RequestParam?
As the name suggests, @RequestParam is used to get the request parameters from URL, also known as query parameters, while @PathVariable extracts values from URI.
For example, if the incoming HTTP request to retrieve a book on topic "Java" is http://localhost:8080/shop/order/1001/receipts?date=12-05-2017, then you can use the @RequestParam annotation to retrieve the query parameter date and you can use @PathVariable to extract the orderId i.e. "1001" as shown below: @RequestMapping(value="/order/{orderId}/receipts", method = RequestMethod.GET) public List listUsersInvoices( @PathVariable("orderId") int order, @RequestParam(value= "date", required = false) Date dateOrNull) { ... }
What is a Bean in Spring and Explain the different scopes of bean in Spring?
A Bean is an object that is instantiated, assembled and managed by a Spring IoC container.
Explain the role of DispatcherServlet and ContextLoaderListener
DispatcherServlet (the receptionist) is the front controller in the Spring MVC application as it loads the spring bean configuration file and initializes all the beans that have been configured.
ContextLoaderListener is the listener used to start up and shut down the WebApplicationContext in Spring root.
What is the difference between constructor injection and setter injection?
Constructor Injection
No partial injection Doesn't override setter property Creates new instance if any modification occurs Better for too many properties ---
Setter Injection
Partial injection
Overrides the constructor property if both are defined
Doesn’t create new instance if you change the property value
Better for few properties
What is autowiring in spring?
Autowiring enables the programmer to inject the bean automatically. We don’t need to write explicit injection logic.
How to handle exceptions in Spring MVC Framework?
Spring MVC Framework provides the following ways to help achieve robust exception handling:
○ Controller Based - we can define exception handler methods in our controller classes.
○ Global Exception Handler - Exception Handling is a cross-cutting concern and Spring provides.
○ HandlerExceptionResolver - Any Spring bean declared in the DispatchServlet’s application context that implements HandlerExceptionResolver will be used to intercept and process any exception raised in the MVC systema and not handled by a Controller.
How to integrate Spring and Hibernate Frameworks?
We can use Spring ORM (Object relationship mapping) module to integrate Spring and Hibernate frameworks. Spring ORM also provides support for using Spring declarative transaction management
What are REST API’S?
A way to interact with a computer or system to retrieve information or perform a function. An API helps you communicate what you want to that system so it can understood and fulfill the request. Like a waitor in a restaurant.
REpresentational State Transfer (REST) is an architectural style that defines a set of constraints to be used for creating web services. REST API is a way of accessing web services in a simple and flexible way without having any processing. REST technology is generally preferred to the more robust Simple Object Access Protocol (SOAP) technology because REST uses less bandwidth, is simple and flexible, making it more suitable for internet usage. It’s used to fetch or give some information from a web service. All communication done via REST API uses only HTTP request.
What are the 5 REST methods?
In HTTP there are five methods which are commonly used in a REST based Architecture i.e., POST, GET, PUT, PATCH, and DELETE (these correspond to create, read, update, and delete (or CRUD) operations respectively).
Describe the 5 REST methods
§ GET: The HTTP GET method is used to read (or retrieve) a representation of a resource. In the safe path, GET returns a representation in XML or JSON and an HTTP response code of 200 (OK). In an error case, it most often returns a 404 (NOT FOUND) or 400 (BAD REQUEST).
§ POST: The POST verb is most-often utilized to create new resources. In particular, it’s used to create subordinate resources. That is, subordinate to some other (e.g. parent) resource. On successful creation, return HTTP status 201, returning a Location header with a link to the newly-created resource with the 201 HTTP status. NOTE: POST is neither safe nor idempotent. § PUT: It is used for updating the capabilities. However, PUT can also be used to create a resource in the case where the resource ID is chosen by the client instead of by the server. In other words, if the PUT is to a URI that contains the value of a non-existent resource ID. On successful update, return 200 (or 204 if not returning any content in the body) from a PUT. If using PUT for create, return HTTP status 201 on successful creation. PUT is not safe operation but it’s idempotent. § PATCH: It is used for modify capabilities. The PATCH request only needs to contain the changes to the resource, not the complete resource. This resembles PUT, but the body contains a set of instructions describing how a resource currently residing on the server should be modified to produce a new version. This means that the PATCH body should not just be a modified part of the resource, but in some kind of patch language like JSON Patch or XML Patch. PATCH is neither safe nor idempotent. § DELETE: It is used to delete a resource identified by a URI. On successful deletion, return HTTP status 200 (OK) along with a response body.