21+ Spring MVC + REST Interview Questions Answers for Beginners and Experienced Developers
Tags: Programming, Software Development, Java, Spring, Coding

21+ Spring MVC + REST Interview Questions Answers for Beginners and Experienced Developers javinpaul Follow Nov 3 · 13 min read

Hello guys! if you are preparing for Java and Spring interviews or Spring certification and looking for some frequently asked Spring MVC and REST interview questions then you have come to the right place. Earlier, I have shared best Spring MVC courses and books and today, I am going to share top 22 Spring Interview Questions for Java developers applying for web developer roles..

Since the Spring Framework is the most popular and standard framework for developing Java web applications and RESTful web services, a good knowledge of Spring core and Spring MVC is expected from any senior Java developer.

But, if the job description mentions REST and web services, you also need to be aware of how to develop RESTful web services using the Spring Framework.

From Spring 3.1, the framework has been enhanced a lot to support many features needed for the RESTFul API. The HTTPMessageConverter can convert your HTTP response to JSON or XML just by detecting a relevant library in the classpath, like Jackson and JAXB.

Spring also provides customized annotations for RESTful Web Services, like @RestController, which can make your Controller REST more aware, so that you don’t need to do common stuff required by every single REST API, like converting the response to JSON.

A deep knowledge of Spring Security is also mandatory for developing security for RESTful web services in the real world. Since you cannot make life a non-trivial REST API without security, a good knowledge of security basics, HTTP basic authentication, digest authentication, OAuth, and JWT is very important.

By the way if you are new to Spring MVC and Spring Framework in general, then I highly recommend you to join a good, comprehensive Spring course like this Spring 5: Beginner to Guru resource to learn the basics first. This will help you to answer this question better and also to do well on both Spring certification and interviews.

Top 22 Spring MVC + REST Web Service Interview Questions with Answers

Here are a couple of frequently asked questions about using REST web services in the Spring Framework.

1. When do you need @ResponseStatus annotation in Spring MVC?

This is a good question for 3 to 5 years as an experienced Spring developer. The @ResponseStatus annotation is required during error handling in Spring MVC and REST. Normally, when an error or exception is thrown at the server-side, the webserver returns a blanket HTTP status code 500 — Internal server error.

This may work for a human user but not for REST clients. You need to send them the proper status code, like 404, if the resource is not found. That’s where you can use them @ResponseStatus annotation, which allows you to send custom HTTP status codes along with proper error message in case of an exception.

In order to use it, you can create custom exceptions and annotate them using the @ResponseStatus annotation and proper HTTP status code and reason.

When such exceptions are thrown from the controller’s handler methods and not handled anywhere else, then the appropriate HTTP response with the proper HTTP status code is sent to the client.

For example, if you are writing a RESTful web service for a library that provides book information, then you can use @ResponseStatus to create an exception that returns the HTTP response code 404 when a book is not found instead of the Internal Server Error (500), as shown below:

@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="No such Book") // 404

public class BookNotFoundException extends RuntimeException { // ... }

If this exception is thrown from any handler method, then the HTTP error code 404 with the reason “No such Book” will be returned to the client.

If you are not familiar with the basics concepts of Spring MVC, Security, and REST, I suggest you go through these REST with Spring and Learn Spring Security courses to gain some experience before your next job interview. These two courses are specially designed to provide you with some real-world experience to boost both your knowledge and experience with Spring MVC, REST, and Spring Security.

2. What does @RequestMapping annotation do? (answer)

The @RequestMapping annotation is used to map web requests to Spring Controller methods. You can map a request based upon HTTP methods, e.g. GET, POST, and various other parameters.

For example, if you are developing a RESTful web service using Spring, then you can use, produce, and consume property along with media type annotations to indicate that this method is only used to produce or consume JSON, as shown below:

@RequestMapping

(method = RequestMethod.POST, consumes="application/json")

public Book save(@RequestBody Book aBook) {

return bookRepository.save(aBook);

}

Similarly, you can create other handler methods to produce JSON or XML. If you are not familiar with these annotations, then I suggest you join this Spring MVC For Beginners course on Udemy to learn the basics.

3. Is @Controller a stereotype? Is @RestController a stereotype? (answer)

Yes, both @Controller and @RestController are stereotypes. The @Controller is actually a specialization of Spring's @Component stereotype annotation. This means that the class annotated with the @Controller will also be automatically detected by the Spring container, as part of the container's component scanning process.

And, the @RestController is a specialization of the @Controller for the RESTful web service. It not only combines the @ResponseBody and @Controller annotations, but it also gives more meaning to your controller class to clearly indicate that it deals with RESTful requests.

Your Spring Framework may also use this annotation to provide some more useful features related to REST API development in the future.

4. When do you need @ResponseBody annotation in Spring MVC? (answer)

The @ResponseBody annotation can be put on a method to indicate that the return type should be written directly to the HTTP response body (and not placed in a Model, or interpreted as a view name).

For example:

@RequestMapping(path = "/hello", method = RequestMethod.PUT)

@ResponseBody

public String helloWorld() {

return "Hello World";

}

Alternatively, you can also use the @RestController annotation instead of the @Controller annotation. This will remove the need for using @ResponseBody because, as discussed in the previous answer, it comes automatically with the @RestController annotation.

5. What does @PathVariable do in Spring MVC? Why it’s useful in REST with Spring? (answer)

This is one of the useful annotations from Spring MVC that allows you to read values from the URI, like query parameter. It’s particularly useful in case of creating RESTful web service using Spring, because, in REST, resource identifiers are part of the URI. This question is normally asked by experienced Spring MVC developers with 4 to 6 years of experience.

For example, this URL can be helpful if you want to learn how to extract the id, then you can use the @PathVariable annotation of Spring MVC. If you are not familiar with Spring MVC annotations, then Spring MVC For Beginners: Build Java Web App in 25 Steps is a good place to start.

6. What is the difference between @Controller and @RestController in Spring MVC? (answer)

There are many differences between them @Controller and @RestController annotations, as discussed in my earlier article (see the answer for more!), but the most important one is that with the @RestController you get the @ResponseBody annotation automatically, which means you don't need to separately annotate your handler methods with the @ResponseBody annotation.

This makes the development of RESTful web services easier using Spring. You can see here to learn more about Spring Boot and how it can help you to create Spring MVC based web applications.

7. What are the advantages of the RestTemplate in Spring MVC? (answer)

The RestTemplate class is an implementation of the Template method pattern in the Spring framework. Similar to other popular template classes, like the JdbcTemplate or JmsTempalte , it also simplifies the interaction with RESTful web services on the client side. You can use it to consume a RESTful web servicer very easily, as shown in this RestTemplate example.

8. Where do you need @EnableWebMVC? (answer)

The @EnableWebMvc annotation is required to enable Spring MVC when Java configuration is used to configure Spring MVC instead of XML. It is equivalent to <mvc: annotation-driven> in an XML configuration.

It enables support for the @Controller -annotated classes that use @RequestMapping to map incoming requests to handler methods that are not already familiar with Spring's support for Java configuration. The Spring Master Class on Udemy is a good place to start.

9. What is an HttpMessageConverter in Spring REST?

An HttpMessageConverter is a strategy interface that specifies a converter that can convert from and to HTTP requests and responses. Spring REST uses this interface to convert HTTP responses to various formats, for example, JSON or XML.

Each HttpMessageConverter implementation has one or several MIME Types associated with it. Spring uses the "Accept" header to determine the content type that the client is expecting.

It will then try to find a registered HTTPMessageConverter that is capable of handling that specific content-type and use it to convert the response into that format before sending it to the client. If you are new to Spring MVC, see this Spring 5: Beginner to Guru resource to learn the basics.

10. How to create a custom implementation of the HttpMessageConverter to support a new type of request/responses?

You just need to create an implementation of the AbstractHttpMessageConverter and register it using the WebMvcConfigurerAdapter#extendMessageConverters() method with the classes that generate a new type of request/response.

11. Do you need Spring MVC in your classpath for developing RESTful Web Service? (answer)

This question is often asked by Java programmers with 1 to 2 years of experience in Spring. The short answer is: yes — you need Spring MVC in your Java application’s classpath to develop RESTful web services using the Spring framework.