Spring and HTTP Flashcards

1
Q

What is the difference between Spring and Spring boot?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Is spring boot a backend?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Spring?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the different modules/features of the Spring framework?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

List some of the important annotations in annotation-based Spring configuration

A

@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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between @PathVariable and @RequestParam?

A

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) { ... }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a Bean in Spring and Explain the different scopes of bean in Spring?

A

A Bean is an object that is instantiated, assembled and managed by a Spring IoC container.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain the role of DispatcherServlet and ContextLoaderListener

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between constructor injection and setter injection?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is autowiring in spring?

A

Autowiring enables the programmer to inject the bean automatically. We don’t need to write explicit injection logic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to handle exceptions in Spring MVC Framework?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to integrate Spring and Hibernate Frameworks?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are REST API’S?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the 5 REST methods?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe the 5 REST methods

A

§ 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Idempotence?

A

Idempotence: An idempotent HTTP method is a HTTP method that can be called many times without different outcomes. It would not matter if the method is called only once, or ten times over. The result should be the same. Again, this only applies to the result, not the resource itself.

17
Q

What are MOST COMMON HTTP CODES

A

HTTP Response Codes indicate whether specific HTTP requests have been successfully completed. Basically, it’s an indicator of whether a web page has loaded successfully. Responses are grouped into five classes: informational responses, successful responses, re-directs, client errors, and server errors.

	§ 1xxs – Informational responses: The server is thinking through the request.
	§ 2xxs – Success! The request was successfully completed and the server gave the browser the expected response.
	§ 3xxs – Redirection: You got redirected somewhere else. The request was received, but there’s a redirect of some kind.
	§ 4xxs – Client errors: Page not found. The site or page couldn’t be reached. (The request was made, but the page isn’t valid — this is an error on the website’s side of the conversation and often appears when a page doesn’t exist on the site.) 5xxs – Server errors: Failure. A valid request was made by the client but the server failed to complete the request.
18
Q

Why use ResponseEntity as the method type?

A
ResponseEntity is a helper class to fully describe the response, including the status code, headers, and body. 
	It makes it easy to set appropriate values without trying to remember what a value should be. 
	For example, you don't need to know what the status code not found is. 
	Similarly, it prohibits you from adding a body if you set the status to no content.
You use ResponseEntity.of() within the findAll method.  This is a shortcut for creating a ResponseEntity with either a valid body and the 200 OK status,  or no body and a 404 Not Found status.