Spring Boot and REST API Interview Questions

Spring/Spring Boot

  1. What is Spring?
    • Spring is a Java EE Framework for building applications. It offers the following:
      • Manages object life cycles in dependencies for components & services in your code through the Application context & Dependency Injection
      • Facilitates database connectivity, querying, transaction mgmt & reduces JDBC coding
      • Spring MVC to build web applications and REST APIs
  2. What is Spring MVC?
    • Spring MVC allows you to create web applications and REST APIs using the same Spring application model and dependency injection concepts.
  3. What is the Scope in Spring?
    • The core of spring framework is it’s bean factory and mechanisms to create and manage such beans inside Spring container. The beans in spring container can be created in five scopes i.e. singleton, prototype, request, session and global-session. They are called spring bean scopes.
      1. Singleton – Scopes a single bean to a single object instance per Spring IoC container.
      2. Prototype – Scopes a single bean definition to any number of object instances.
      3. Request – Scopes a single bean definition to the lifecycle of a single HTTP request
      4. Session – Scopes a single bean definition to the lifecycle of an HTTP Session.
      5. Global-session – Scopes a single bean definition to the lifecycle of a global HTTP Session.
  4. What is Dependency Injection and how would you achieve that in Spring? 
    • Dependency injection is a pattern used to create instances of objects that other objects rely on. This leads to code being loosely coupled. Two ways of achieving it include:
      1. Constructor-Based Dependency Injection
      2. Setter-Based Dependency Injection
  5. What is Inversion of Control and autowiring?
    • Inversion of Control is a principle in software engineering by which the control of objects or portions of a program is transferred to a container or framework instead of the programmer.
    • Autowiring feature of spring framework enables you to inject the object dependency implicitly. It internally uses setter or constructor injection. Essentially, it allows dependency injection.
  6. How do you start a regular spring application?
    1. To build a java app with Spring, you can use different build tools like maven, cradle ant.
      • Example: Create a maven project, add spring dependency.
    2. Write all layers, controller, services, repository etc.
  7. What are the ways to create beans in spring?
    • Declaring a bean in XML configuration file
    • Using @Component annotation
    • Using @Configuration annotation
  8. Why do we need Spring Boot?
    • Spring Boot is a Java-based framework used to quickly build production ready spring applications.
  9. How is Spring different from Spring Boot?
    • Spring framework is used to write enterprise java applications. Spring required a lot of configuration to get it to be production ready.
    • Spring boot offers that ability to quickly create a Spring application with features to lessen the configuration required through a combination of Auto Configuration and Starter Projects.
  10. What dependency do you need to add to a Spring Boot Project? What about for a database? 
    • Spring Boot offers several starter dependencies which can account for all dependencies needed for any given task. For example, if you wanted to make a web application using the Spring MVC you could use spring-boot-starter-web dependency
    • If you wanted to use JDBC in your project, you can use the starter dependency of spring-boot-starter-jdbc.
  11. What is a Servlet?
    • Servlets are Java classes which are used to handle requests, process them and reply back with a response. Simply a way to generate dynamic web pages. 
    • To become a servlet, your class must extend HTTPServlet and you should configure your details in your Deployment Descriptor web.xml OR use the annotation @WebServlet(“/html”).
  12. How to perform testing in Spring Boot?
    • There are a variety of tests you can write and use in Spring Boot to make sure your application is doing what its meant to do. To do any of this testing, you have to make sure you have the starter dependency for testing in your pom.xml file which is spring-boot-starter-test.
      1. Integration Testing with @DataJpaTest
      2. Mocking with @MockBean
      3. Unit Testing with @WebMvcTest
      4. Integration Testing with @SpringBootTest
      5. Auto-Configured Tests
  13. How to reverse a string in Spring Boot?
    1. Using the get request, retrieve the string data
    2. In the service class, include the String builder class and insert the string in the reverse method
    3. Post the string using the http post method in the controller
  14. How do you  persist/retrieve data with spring boot? 
    1. Add JPA 2 and Hibernate to our project
    2. Add required annotated entity we want persisted and add an id
    3. Create a repository
    4. Create a class or service to use the repository
  15. What are annotations used for?
    • Use of annotations provide us capabilities in how we configure the behaviors of the Spring Framework. Annotations are metadata or data about data. An annotation indicates that the declared element should be processed in some special way by a compiler, development tool, deployment tool, or during runtime.

Rest API

  1. What is a REST API?
    • A RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data.
    • A RESTful API — also referred to as a RESTful web service — is based on representational state transfer (REST) technology, an architectural style and approach to communications often used in web services development.
  2. Tell me about the conditions of a RESTful webservice?
    1. Client-Server
      • A RESTful API will have a client-server architecture. The client requests resources while not being concerned with data storage. The server on the other hand holds the resources while not being concerned with the user interface or state. They can work independently of each other. This improves the portability of the UI across platforms as well as improving scalability by simplifying the server components.
    2. Stateless
      • Session state is kept entirely on the client because every request must contain all needed information to understand the request. It cannot take advantage of any stored context on the server.
    3. Cacheable
      • Every response will include  information on if the response is cacheable or not and for how long the client can reuse that response data. This helps to eliminate some client-server interactions which improves availability and performance and will keep your data up to date a majority of the time.
    4. Uniform Interface
      • There is a uniform way of interacting with the server, regardless of the kind of application that’s trying to access it–a key constraint between a RESTful API and a non-RESTful API. This is achieved by following its own set of guidelines
        1. Resource-Based
        2. Manipulation of Resources Through Representations
        3. Self-descriptive Messages
        4. Hypermedia as the Engine of Application State (HATEOAS)
    5. Layered System
      • Architecture is composed of several hierarchical layers with the caveat that each component doesn’t know about any of the other layers with the exception of the immediate layer it is interacting with.
    6. Code on Demand (optional) 
      • Servers can provide executable code in the form of scripts or applets  to the client. It reduces the number of features that need to be pre-implemented.
  3. How do you conduct content negotiation in REST API?
  4. How do you build a REST API using Spring Boot?
    • Step 1: Create the Spring Boot Project.
    • Step 2: Define Database configurations.
    • Step 3: Create an Entity Class.
    • Step 4: Create JPA Data Repository & Service layer.
    • Step 5: Create Rest Controllers and map API requests.
    • Step 6: Create Unit Testing for API requests and run the unit testing.
    • Step 7: Build and run the Project.
  5. What methods would you write in your Controller?
    • Controller methods annotated with http request mapping, these methods will handle http request on the specified url. You should have controller methods to handle CRUD operation request.
    • @GetMapping, @PostMapping, @PutMapping, @DeleteMapping
  6. What do you do on your controller layer?
    • The job of @Controller is to create a Map of model object and find a view
  7. What is a RestController?
    • @RestController simply return the object and object data is directly written into HTTP response as JSON or XML
  8. What is the difference between @RestController and @Controller?
    • While the @RestController is a special annotation for RESTful webservices, returns the object and object data that was converted into a HTTP response as JSON or XML, the @Controller simply marks a class as a Spring MVC controller.
  9. What annotations do you use in your rest controller?
    • @RestController would be used to annotate the class while @GetMapping, @PostMapping, @PutMapping and @DeleteMapping would be used to annotate methods written to do those corresponding CRUD requests. You could also use @RequestMapping instead to pass it the correct parameter to tell it if it’s a GET, PUT, POST or DELETE crud operation. @RequestParam can also be used to pass query parameters and form parameters.
  10. How would you return status 201 from a REST API?
    • HTTP convey the results of a client’s request. 2xx: are in the Success Category. Indicates that the client’s request was accepted successfully. 201(created) A rest API responds with the 201 status code whenever a resource is created inside a collection.
  11. What would a ResponseEntity consist of?
    • A ResponseEntity represents the HTTP response, so it consists of the headers, body, and status code.
  12. Can a REST API provide both JSON and XML response?
    • Yes, but you cannot provide both JSON and XML at the same time.
  13. What is CORS?
    • CORS stands for cross-origin resource sharing, which allows restricted resources to be accessed by domains outside of the domain which is serving the resource.
  14. How do you fix a CORS?
    • In the server, you specify both the domain you want to allow as well as the REST methods you want allow.
  15. How would you receive and reverse a string using a Rest API?
    • In the controller class we have post-mapping , handles post requests . So we send a JSON object that contains a string and then to reverse it we go to service and we create a method that will reverse that string such as stringBuilder reverse(). method. to send a string to the API , and reverse it there .

Sources

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.