What is @RequestBody annotation?
Answer: @RequestBody annotation is used to bind a method parameter to incoming HTTP requests with the mentioned URL in @RequestMapping annotation.
Here, HTTP message convertors are used to map HttpRequest body to a domain object, which consequently helps to automatically deserialize the body to a Java object based on header present in the request.
What is @ResponseBody annotation?
Answer: The task of @ResponseBody annotations is to inform the controller that the returned object has been serialized into Json, the returned value will be bound to HttpResponse body.
Here, Spring HTTP message converters serialize the return value to the type specified in HTTP header.
What is @ResponseEntity annotation?
Answer: @ResponseEntity, as the name suggests, is an annotation that represents the HTTP response entity. @ResponseEntity contains constructors that allow you to specify status code, body, header, and other information.
What is @pathVariable?
Answer: @PathVariable annotation is used to directly extract information from the URI.
What is MediaType?
Answer: MediaType is used to specify the type of data that is to be produced or consumed by the controller. This narrows down and aids with mapping.
What is REST API?
Answer: Restful API is one of the favorite Spring Boot Interview Questions and Answers of recruiters to interrogate a fresher candidate.
REST API or RESTful API is used to transfer http requests such as Get, Post, Put, and Delete. REST or Representational Transfer is a technology that imitates communication style used for web services.
Difference between POST and PUT methods?
Answer: Basically, Put is used to replace all the target resources with the selected/requested payload. On the other hand, Post method is used to submit or send data to the specific server or resource such as file upload, information, or changing the current status.
An explicitly created object suggest use of the Put method while server-specified name shows the use of Post method.
Put allows you to Put objects more than a single time and also allows creation or updation of resources using the exact object or URL.
On the other hand, Post allows multiple requests at the same time to make changes to an object or URL.
What is @Autowired annotation and its usage?
Answer: Same as the @Required annotation, @Autowired annotation can be used to bind or autowire bean with the setter method. In other words, one can implicitly define object dependency. It provides developers with control regarding autowiring.
How many ways the autowired can mode?
Answer:
• No: It’s the default option. Defining not to autowire, which is set using ref attribute.
• byName: This makes use of property name for autowiring. If bean name matches other bean property name, autowiring is done.
• byType: Same way as the byName mode, if the bean data type is shown to be compatible with other bean property, autowiring is done.
• Constructor: byType mode defined in constructor.
• Autodetect: Finds the default constructor for autowiring. If not found, byType autowiring is performed.
What are the limitations of autowiring mode?
Answer:
• If constructor contains explicit dependencies, they will override autowiring instructions.
• Possibilities of Overriding: Dependencies can be easily defined using constructor-arg or property tags to override the autowiring.
• String, integer, and similar data type properties have to be defined manually and cannot be autowired.
What is spring-boot-starter-data-jpa?
Answer: Spring Boot provides spring-boot-starter-data-jpa, which is one of the Spring Boot Starters, to easily connect relational database with Spring MVC applications.
org.springframework.boot
spring-boot-starter-data-jpa
How does JPA Work?
Answer: JPA stands for Java Persistence API, which is used as an API that allows the developers to work with objects. When you deal with a relational database, you have to make use of SQL statements in order to retrieve information for certain queries. JPA eliminates the need of using SQL statements and allows the developers to deal with objects without SQL statement requisite. Developers can perform actions such as accessing, modifying, managing, and persisting information.
A JPA persistence provider can create a database schema by following the information provided in metadata. Here, JPA metadata has to be defined in the Java class. Metadata can also be defined using XML. JPA can handle both static and dynamic object-based queries.Get updated spring boot interview questions
What is the difference between JPA and Hibernate?
Answer: While JPA and Hibernate both are completely different entities, they are interconnected in certain ways. JPA is an API that provides developers with specifications and guidelines for implementation. However, it is not an implementation and will require a tool that can implement the specifications.
Consequently, Hibernate comes into picture, which is nothing but a tool that follows the guideline as well as compulsory and optional specifications defined by JPA and makes it functional.
Using Hibernate for JPA implementation also allows you to switch over other implementation tools easily, which is not possible if used Hibernate alone.