Wednesday, April 24, 2024
HomeJavaHigh 20 Spring and REST Interview Questions Solutions for Java/JEE Programmers

High 20 Spring and REST Interview Questions Solutions for Java/JEE Programmers


Hi there guys, I’ve been sharing lots of REST with Spring tutorials from final a few weeks and as we speak, I’m going to share a few of the incessantly requested Spring and REST interview inquiries to Java builders making use of for Internet developer roles. Since Spring Framework is the most well-liked and the usual framework for growing Java internet utility and RESTful Internet Companies, a very good information of Spring core and Spring MVC is anticipated from any senior Java developer, but when the job description point out about REST and Internet Companies, you additionally want to concentrate on learn how to develop RESTful Internet Companies utilizing Spring Framework.

From Spring 3.1, the framework has been enhanced quite a bit to assist many options wanted for RESTFul API out-of-the-box like HTTPMessageConverter can convert your HTTP response to
JSON or XML by simply detecting related library in classpaths like Jackson and JAXB.

Spring additionally gives personalized annotations for RESTful Internet Companies like @RestController which may make your Controller REST conscious, so that you just need not do frequent stuff required by each single REST API like changing the response to JSON.

Good information of Spring Safety can be obligatory for growing and safety RESTful Internet Companies in the true world. Since you can not make life a non-trivial REST API with out safety, a very good information of safety fundamentals like  HTTP fundamental authentication, digest authentication, OAuth, and JWT is essential.

Btw, In case you are not accustomed to that idea, I recommend you undergo REST with Spring and Spring Framework 5: Newbie to Guru course to achieve some expertise earlier than your Interview. These two programs are specifically designed to offer you some real-world expertise to spice up each your information and expertise with Spring MVC, REST, and Spring Safety.

20 Spring REST Internet Service Interview Questions 

Listed below are a few incessantly requested questions on utilizing REST Internet Companies in Spring Framework.

1. What does REST stand for? (reply)

REST stands for REpresentational State Switch, which makes use of HTTP protocol to ship knowledge from consumer to server e.g. a guide within the server will be delivered to the consumer utilizing JSON or XML.

2. What’s a useful resource? (reply)

A useful resource is how knowledge is represented in REST structure. By exposing entities because the useful resource it permits a consumer to learn, write, modify, and create assets utilizing HTTP strategies like GET, POST, PUT, DELETE, and so on.

3. What are protected REST operations? (reply)

REST API makes use of HTTP strategies to carry out operations. Among the HTTP operations which do not modify the useful resource on the server are often known as protected operations e.g. GET and HEAD. However, PUT, POST, and DELETE are unsafe as a result of they modify the useful resource on the server.

4. What are idempotent operations? Why is idempotency vital? (reply)

There are some HTTP strategies e.g. GET which produce identical response regardless of what number of instances you utilize them e.g. sending a number of GET request to the identical URI will end in identical response with none side-effect therefore it is named idempotent.

However, the POST shouldn’t be idempotent as a result of when you ship a number of POST requests, it can end in a number of useful resource creation on the server, however once more, PUT is idempotent if you’re utilizing it to replace the useful resource.

Even, a number of PUT requests to replace a useful resource on a server will give the identical finish end result. You possibly can additional take the HTTP Fundamentals course by Pluralsight to be taught extra about idempotent strategies of the HTTP protocol and HTTP normally.

5. Is REST scalable and/or interoperable? (reply)

Sure, REST is Scalable and interoperable. It does not mandate a selected alternative of know-how both on the consumer or server finish. You should use Java, C++, Python or JavaScript to create RESTful Internet Companies and Eat them on the consumer finish. I recommend you learn a very good guide on REST API e.g. RESTful Internet Companies to be taught extra about REST.

6. What are the benefits of the RestTemplate? (reply)

The RestTemplate class is an implementation of the Template methodology sample within the Spring framework. Much like different common template courses like JdbcTemplate or JmsTempalte, it additionally simplifies the interplay with RESTful Internet Companies on the client-side. You should use it to devour a RESTful Internet Servicer very simply as proven on this instance.

7. Which HTTP strategies does REST use? (reply)

REST can use any HTTP strategies however the most well-liked ones are GET for retrieving a useful resource, POST for making a useful resource, PUt for updating the useful resource and DELETE for eradicating a useful resource from the server.

8. What’s an HttpMessageConverter in Spring REST? (reply)

An HttpMessageConverter is a Technique interface that specifies a converter that may convert from and to HTTP requests and responses. Spring REST makes use of this interface to transform HTTP response to varied codecs e.g. JSON or XML.

Every HttpMessageConverter implementation has one or a number of MIME Varieties related to it. Spring makes use of the “Settle for” header to find out the content material kind the consumer is anticipating.

It’s going to then attempt to discover a registered HTTPMessageConverter that’s able to dealing with that particular content-type and use it to transform the response into that format earlier than sending it to the consumer.

Top 20 Spring and REST Interview Questions Answers for Java/JEE Programmers

9. Find out how to create a customized implementation of HttpMessageConverter to assist a brand new kind of request/response? (reply)

You simply must create an implementation of AbstractHttpMessageConverter and register it utilizing the WebMvcConfigurerAdapter#extendMessageConverters() methodology with the courses which generate a brand new kind of request/response.

10. Is REST usually stateless? (reply)

Sure, REST API ought to be stateless as a result of it’s primarily based on HTTP which can be stateless. A Request in REST API ought to include all the main points required it to course of i.e. it shouldn’t depend on earlier or subsequent request or some knowledge maintained on the server finish e.g. Classes. REST specification places a constraint to make it stateless and you need to maintain that in thoughts whereas designing your REST API.

11. What does @RequestMapping annotation do? (reply)

The @RequestMapping annotation is used to map internet requests to Spring Controller strategies. You possibly can map requests primarily based upon HTTP strategies just like the GET and POST and varied different parameters. For examples, if you’re growing RESTful Internet Service utilizing Spring then you should utilize produces and consumes property together with media kind annotation to point that this methodology is just used to provide or customers JSON as proven beneath:

@RequestMapping (methodology = RequestMethod.POST, consumes="utility/json")
public E book save(@RequestBody E book aBook) {
   return bookRepository.save(aBook);
}

You possibly can equally create different handler strategies to provide JSON or XML. In case you are not accustomed to these annotations then I recommend you be a part of Spring MVC For Rookies course on Udemy to be taught from scratch.

12. Is @Controller a stereotype? Is @RestController a stereotype? (reply)

Sure, each @Controller and @RestController are stereotypes. The @Controller is definitely a specialization of Spring’s @Element stereotype annotation. Because of this class annotated with @Controller can even be routinely be detected by Spring container as a part of the container’s part scanning course of.

And, @RestController is a specialization of @Controller for RESTful internet service. It not solely combines @ResponseBody and @Controller annotation but in addition offers extra which means to your controller class to obviously point out that it offers with RESTful requests.

Spring Framework might also use this annotation to offer some extra helpful options associated to REST API improvement sooner or later.

13. What’s the distinction between @Controller and @RestController?  (reply)

There are various variations between @Controller and @RestController as mentioned in my earlier article (see the reply) however an important one is that with @RestController you get the @ResponseBody annotation routinely, which suggests you need not individually annotate your handler strategies with @ResponseBody annotation. 

This makes the event of RESTful internet service simpler utilizing Spring. You possibly can see right here be taught extra in regards to the distinction between @RestController and @Controller annotations in Spring API.

Top 20 Spring REST Interview Questions Answers for Java Programmers

14. When do you want @ResponseBody annotation in Spring MVC? (reply)

The @ResponseBody annotation will be placed on a way to signifies that the return kind ought to be written on to the HTTP response physique (and never positioned in a Mannequin, or interpreted as a view title).

For instance:

@RequestMapping(path = "/whats up", methodology = RequestMethod.PUT)
@ResponseBody
public String helloWorld() {
   return "Hi there World";
}


Alternatively, it’s also possible to use @RestController annotation as an alternative of @Controller annotation. This can take away the necessity for utilizing @ResponseBody as a result of as mentioned within the earlier reply, it comes routinely with @RestController annotation.

15. What does @PathVariable do in Spring MVC? Why it is helpful in REST with Spring? (reply)

It is one of many helpful annotations from Spring MVC which lets you learn values from URI like question parameter. It is notably helpful in case of making RESTful internet service utilizing Spring as a result of in REST useful resource identifiers are a part of URI. This query is generally requested skilled Spring MVC builders e.g. 4 to six years of expertise.

For instance, within the URL http://myapp.com/books/101 if you wish to extract 101 the id, then you should utilize @PathVariable annotation of Spring MVC.  In case you are not accustomed to Spring MVC annotations then Spring MVC For Rookies: Construct Java Internet App in 25 Steps is an effective place to start out with.

Spring MVC REST Interview Questions

16. What’s the HTTP standing return code for a profitable DELETE assertion? (reply)

There isn’t any strict rule with respect to what standing code your REST API ought to return after a profitable DELETE i.e it may well return 200 Okay or 204 No Content material. Typically, if the DELETE operation is profitable and the response physique is empty return 204. If the DELETE request is profitable and the response physique is NOT empty, return 200

17. What does CRUD imply? (reply)

CRUD is a brief type of Create, Learn, Replace and Delete. In REST API, the POST is used to create a useful resource, GET is used to learn a useful resource, PUT is used to up to date a useful resource and DELETE is used to take away a useful resource from the server. This one is one other newbie degree Spring MVC questions for 1 to three years skilled programmers

18. The place do you want @EnableWebMVC? (reply)

The @EnableWebMvc annotation is required to allow Spring MVC when Java configuration is used to configure Spring MVC as an alternative of XML. It’s equal to <mvc: annotation-driven>  in XML configuration.

It permits assist for @Controller-annotated courses that use @RequestMapping to map incoming requests to handler strategies not already accustomed to Spring’s assist for Java configuration, Spring Grasp Class on Udemy is an effective place to start out.

19. When do you want @ResponseStatus annotation in Spring MVC? (reply)

An excellent questions for 3 to five years skilled spring builders. The @ResponseStatus annotation is required throughout error dealing with in Spring MVC and REST. Usually when an error or exception is thrown on the server-side, internet server returns a blanket HTTP standing code 500 – Inside server error.

This may occasionally work for a human person however not for REST shoppers. It is advisable to ship them a correct standing code like 404 if the useful resource shouldn’t be discovered. That is the place you should utilize @ResponseStatus annotation, which lets you ship customized HTTP standing code together with correct error message in case of Exception.

With a view to use it, you may create customized exceptions and annotated them utilizing @ResponseStatus annotation and correct HTTP standing code and purpose.

When such exceptions are thrown from controller’s handler strategies and never dealt with wherever else, then acceptable HTTP response with the correct HTTP standing code, which you may have set is shipped to the consumer.

For instance, if you’re writing a RESTful Internet Service for a library which gives guide data then you should utilize @ResponseStatus to create Exception which returns HTTP response code 404 when a guide shouldn’t be discovered as an alternative of Inside Server Error (500), as proven beneath:

@ResponseStatus(worth=HttpStatus.NOT_FOUND, purpose="No such E book")  
 public class BookNotFoundException extends RuntimeException {
     
 }

If this Exception is thrown from any handler methodology then HTTP error code 404 with purpose “No such E book” will likely be returned to the consumer.

20. Is REST safe? What are you able to do to safe it? (reply)

This query is generally requested with skilled Java programmers e.g. 2 to five years expertise with each REST and Spring. Safety is a broad time period, it may imply safety of message which is supplied by encryption or entry restriction which is supplied utilizing authentication and authorization. REST is generally not safe however you may safe it by utilizing Spring safety.

On the very least you may allow HTTP fundamental authentication by utilizing HTTP in your Spring safety configuration file. Equally, you may expose your REST API utilizing HTTPS if the underlying server helps HTTPS.

Spring REST Interview Questions with Answers

21. Does REST work with transport layer safety (TLS)? (reply)

TLS or Transport Layer Safety is used for safe communication between consumer and server. It’s the successor of SSL (Safe Socket Layer). Since HTTPS can work with each SSL and TLS, REST may work with TLS.

Really, REST says something about Safety, it is as much as the server which implements that. The identical RESTful Internet Service will be accessed utilizing HTTP and HTTPS if the server helps SSL.

In case you are utilizing Tomcat, you may see right here to be taught extra about learn how to allow SSL in Tomcat.

22. Do you want Spring MVC in your classpath for growing RESTful Internet Service? (reply)

This query is commonly requested Java programmers with 1 to 2 years of expertise in Spring. The quick reply is Sure, you want Spring MVC in your Java utility’s classpath to develop RESTful internet providers utilizing the Spring framework. It is really Spring MVC which gives all helpful annotations e.g. @RestController, @ResponseCode, @ResponseBody, @RequestBody, and @PathVariable, therefore you will need to spring-mvc.jar or acceptable Maven entry in your pom.xml

That is all about some incessantly requested Spring REST Interview questions for inexperienced persons and skilled Java JEE builders. These questions are additionally very helpful to brush up your information about Spring and REST matters if you will take Spring Skilled Core Certification.  

For those who want extra questions from the Spring certification perspective, you will see that lots of questions on this subject on David Mayer’s Core Spring Simulator, among the best simulators to go Spring certification in the intervening time.

Different Spring tutorials and Sources you might like

Thanks for studying this text, when you like this text then please share with your folks and colleagues. When you’ve got any query which isn’t answered on this checklist then please drop a remark and I will strive my finest to seek out a solution for you.

P. S. – In case you are new into Spring world and wish to be taught key Spring applied sciences and frameworks like Core Spring, Spring MVC, Spring Boot, and Spring Cloud then I recommend you try this checklist of High 5 programs to be taught Spring in-depth, its helpful for each inexperienced persons and skilled Java programmers. 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments