Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Define HttpMessageConverter in terms of Spring REST?

Answer»

HttpMessageConverter is a strategic INTERFACE that specified a converter for conversion between HTTP Requests and responses. Spring REST uses the HttpMessageConverter for converting responses to various data formats like JSON, XML, etc. Spring makes use of the “Accept” header for determining the TYPE of content the client expects. Based on this, Spring would FIND the registered MESSAGE converter interface that is CAPABLE of this conversion.

2.

Is it necessary to keep Spring MVC in the classpath for developing RESTful web services?

Answer»

YES. Spring MVC needs to be on the classpath of the application while developing RESTful web services using Spring. This is because, the Spring MVC provides the necessary annotations like @RestController, @RequestBody, @PATHVARIABLE, etc. HENCE the spring-mvc.jar needs to be on the classpath or the CORRESPONDING Maven entry in the pom.xml.

3.

What does the annotation @PathVariable do?

Answer»

@PathVariable ANNOTATION is used for passing the parameter with the URL that is required to get the data. SPRING MVC PROVIDES support for URL customization for data retrieval using @PathVariable annotation.

4.

What are the differences between the annotations @Controller and @RestController?

Answer»
@Controller @RestController
Mostly used traditional Spring MVC service. Represents RESTful web service in Spring.
It is mostly used in Spring MVC service where MODEL data needs to rendered USING view.It is used in case of RESTful web service that returns object values bound to response body.
If response values NEED to be converted through HttpMessageConverters and sent via response object, EXTRA annotation @ResponseBody needs to be used on the class or the method handlers.The default behavior of the @RestController needs to be WRITTEN on the response body because it is the combination of @Controller and @ResponseBody.
@Controller provides control and flexibility over how the response needs to be sent.@RestController annotation has no such flexibility and writes all the results to the response body.
5.

What is the use of @RequestMapping?

Answer»
  • The annotation is used for mapping requests to specific handler classes or methods.
  • In spring, all the incoming WEB request routing is handled by Dispatcher SERVLET. When it GETS the request, it determines which controller is meant for processing the request by means of request handlers. The Dispatcher Servlet scans all the classes annotated with @Controller. The process of routing requests DEPENDS on @RequestMapping annotations that are declared inside the controller classes and their methods.
6.

Define RestTemplate in Spring.

Answer»

The RestTemplate is the MAIN class meant for the client-side access for Spring-based RESTful services. The communication to the server is accomplished using the REST CONSTRAINTS. This is similar to other template classes such as JdbcTemplate, HibernateTemplate, etc provided by Spring. The RestTemplate provides high-level implementation details for the HTTP Methods like GET, POST, PUT, etc, and gives the methods to communicate using the URI template, URI path params, request/response types, request object, etc as PART of arguments.

  • Commonly used annotations like @GetMapping, @PostMapping, @PutMapping, etc are provided by this class from Spring 4.3. Prior to that, Spring provided (and still provides) @RequestMapping ANNOTATION to indicate what methods were being used.