Showing posts with label spring boot. Show all posts
Showing posts with label spring boot. Show all posts

Saturday, August 8, 2020

Java Technical interview @ ThalesGroup : Round-2

Hi Friends,

In this post, I am sharing interview questions and answers asked in ThalesGroup : Round-2.


Question 1:

What are the tools available for testing web services?

Answer:

  • SOAP UI Tool
  • Poster for Firefox Browser
  • The Postman extension for Chrome

Question 2:

Which java API helps in developing a RESTful web service?

Answer:

There are many frameworks and libraries that a developer can use to create Restful web services in java.

For example , the JAX-RS library is standard way to develop a REST web service.


Also Jersey is another most popular implementations of JAX-RS which offers more than what the specs recommended. There are others like  RESTEasy, RESTLet and Apache CFX.



Question 3:

What is the difference between REST and RESTful?

Answer:

REST based services/architecture vs RESTFul services/architecture:

To differentiate or compare these two, we should know what is REST.

REST [Representational State Transfer]: Is basically an architectural style of development having some principles:

  • It should be stateless
  • It should access all the resources from the server using only URI.
  • It does not have inbuilt encryption
  • It does not have session
  • For performing CRUD operations , it should use HTTP verbs, such as GET, POST, PUT, DELETE, PATCH.
  • It should return the result only in JSON format or XML , atom etc. (lightweight data)


REST based services follow some of the above principles and not all.

RESTFUL services means, it follows all the above principles.

 


Question 4:

Write a sample GET request using Jersey.

Answer:

@GET

@Path("/{empId}")

Employee getEmployee(@PathParam("empId") String empId, @QueryParam("empName") String empName)


Question 5:

Is Spring initializer the only way to create Spring boot projects?

Answer:

No

We can use two approaches:

  • The first one is start.spring.io
  • The other one is : Setting up a project manually
Setting up a maven project manually:

  • In Eclipse, Use File-> New Maven project to create a new project
  • Add dependencies
  • Add the maven plugins
  • Add the Spring boot Application class


Question 6:

Why do we need spring-boot-maven-plugin?

Answer:

Spring-boot-maven-plugin provides a few commands which enable us to package the code as a jar or run the application.

Command in spring-boot-maven-plugin are:

  • Spring-boot: run - Runs the spring boot application
  • Spring-boot : repackage - Repackages the jar/war to be executable
  • Sprint-boot: start and spring-boot: stop - To manage the lifecycle of spring boot application.
  • Spring-boot: build-info - Generates build information that can be used by the Actuator.



Question 7:

How can I enable auto reload of  Spring application with spring boot? 

Answer:

We can enable auto reload of spring application by using spring boot developer tools.

Adding spring boot developer tools to the project is very simple.

Add this dependency to pom.xml:


<dependency>

    <groupId> org.springframework.boot</groupId>

    <artifactId>spring-boot-devtools</artifactId>

    <scope>runtime</scope>

</dependency>

Restart the application.


Question 8:

What is the use of Embedded servers?

Answer:

Think about what you would need to be able to deploy the application on a virtual machine:

  • Step 1: Install Java
  • Step 2: Install the web/application server
  • Step 3: Deploy the application war
What if we want to simplify it?
How about making the server a pat of the application?

We just need a virtual machine with java installed and we would be able to directly deploy the application on the virtual machine.

This idea is the genesis of Embedded servers.
When we create an application deployable , we would embed the server (for example tomcat or jetty) inside the deployable.

Embedded server is when the deployable unit contains the binaries of the server (example tomcat.jar)


 
Question 9:

How can we use profiles to configure environment specific configuration with spring boot?

Answer:

Profile is nothing but a key to identify an environment.

In this example, we will use two profiles

  • dev
  • prod

The default application configuration is present in application.properties. Let’s consider an example.

application.properties

  • basic.value= true
  • basic.message= Dynamic Message
  • basic.number= 100

We would want to customize the application.properties for dev profile. We would need to create a file with name application-dev.properties and override the properties that we would want to customize.

application-dev.properties

basic.message: Dynamic Message in DEV


Similarly you can configure properties for prod profile.

  • application-prod.properties
  • basic.message: Dynamic Message in Prod

Once you have profile specific configuration, you would need to set the active profile in an environment.

There are multiple ways of doing this:

  • Using -Dspring.profiles.active=prod in VM Arguments
  • Use spring.profiles.active=prod in application.properties



Question 10:

Which file is required for application configuration with spring boot?

Answer:

The file name is application.properties.

application.properties can reside anywhere in classpath of the application.



Question 11:

How does spring boot reduces configuration?

Answer:

A Web Application using Spring MVC and JPA (Hibernate):

·         Pom.xml

·         Configure Service/DAO Layer Beans using JavaConfig

·         Application.properties file

·         Log4j.properties file

·         Configure Spring MVC Web Layer Beans

·         Register Spring MVC FrontController Servlet DispatcherServlet

·         Create JPA Entity

·         Create Spring Data JPA repository

·         Create a SpringMVC Controller

·         Create a Thymeleaf View /WEB-INF/views/index.html

 

Using Spring Boot:

·         Pom.xml 

·         Application.properties file

·         Create JPA Entity

·         Create Spring Data JPA repository

·         Create Thymeleaf view

·         Create SpringBootEntryPoint class



That's all for this post.
Thanks for reading!!

Thursday, April 2, 2020

Spring Boot interview @ Sapient

Hi Friends,

Also go through my other interview posts:






Question 1:

What is the use case of Spring Boot?

Answer:

Spring Boot is used while creating microservices. As with increasing features in our applications, we need to create a separate microservice for each new feature and doing entire setup for this including adding all dependencies will  take time. So, in this scenario, Spring Boot is required.

In a monolithic application, we just do setup and dependency addition only once, so in monolithic application, we don't need spring boot.




Question 2:

What are the steps to use Spring-boot-starter-caching ?

Answer:

Steps for using spring-boot-starter-cache are following:

1). Add dependency: 

    <dependency>
        <groupId> org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache </artifactId>
    </dependency>

2). Add @EnableCaching annotation on main Spring Boot Application class.

The @EnableCaching triggers a post-processor that inspects every spring bean for the presence of caching annotations  on public methods. If such an annotation is found, a proxy is automatically created  to intercept the method call and handle the caching behavior accordingly.

The post-processor handles the @Cacheable, @CachePut and @CacheEvict annotations.

Spring boot automatically configures a suitable CacheManager to serve as a provider for the relevant cache.

3). Put @Cacheable and other annotations on methods.

e.g.:

public interface BookRepository{

    Book getByIsbn(String isbn);
}

Use our own implementation if we are not using Spring Data.

public class SimpleBookRepository implements BookRepository{

    @Override
    @Cacheable("books")
    public Book getByIsbn(String isbn){

        return new Book(isbn);
    }

}




Question 3:

What happens when we call SpringApplication.run() method in main class of SpringBoot application?

Answer:

Syntax of the class containing main method looks like code below:

@SpringBootApplication
public class StudentApplication{

    public static void main(String[] args){

        SpringApplication.run(StudentAppplication.class, args);
    }
}

When we run this class as a java application, then our application gets started.

SpringApplication.run() is a static method and it returns an object of ConfigurableApplicationContext.

ConfiguravleApplicationContext ctx = SpringApplication.run(StudentApplication.class, args);

Thus, Spring container gets started once run() method gets called.

Spring container once started is responsible for:


  • Creating all objects: This is done by @ComponentScan. Remember @SpringBootApplication is a combination of @ComponentScan + @Configuration + @EnableAutoConfiguration
  • Dependency injection
  • Managing the lifecycle of all beans
Steps executed under run() method are as follows:

  • Application Context is started.
  • Using application context, auto discovery occurs: @ComponentScan
  • All default configurations are setup.
  • An embedded servlet container is started e.g. Tomcat. No need to setup a separate web server. Note: Embedded servlet container is launched  only if the web is mentioned in a dependency.





Question 4:

What are Spring Boot Actuator endpoints?

Answer:

Spring Boot Actuator is used to monitor and manage application usages in production environment without coding and configuration for any of them.


This monitoring and managing information is exposed via REST like endpoint URL's.

Actuator Endpoints:

/env : Returns list of properties in current environment

/health : Returns application health information.

/beans : Returns a complete list of all the Spring beans in the application.

/trace : Returns trace logs [By default the last 100 HTTP requests]

/dump : It performs a thread dump

/metrics : It shows several useful metrics information like JVM memory used, CPU usage , open files and much more.

Note: These endpoints can be explicitly enabled/disabled.
 
If we need to list all endpoints in the browser:
Just run the application and from browser , use localhost:8080/actuator. It will list 2 endpoints by default : info and health

For other endpoints, we need to expose them manually.

Now, if we want to explore health endpoint, then just open localhost:8080/actuator/health URL and it will just display "status" : "UP"

We can enable or disable the endpoint from application.properties file:

management.endpoint.shutdown.enabled = true

By default all the endpoints are enabled except shutdown endpoint.




Question 5:

By default on which port Spring Boot Actuator runs?

Answer:

On port 8080

We can override that setting by adding application.properties file.



Question 6:

Difference between Spring 2 and Spring 5?

Answer:

Below are the differences between Spring 2 and Spring 5 versions:


  • JDK Baseline update
  • Core framework revision
  • Reactive programming model
  • Core Container updates
  • Testing improvements



Question 7:

What is the difference between @SpringBootApplication and @EnableAutoConfiguration?

Answer:

Following are the differences between @SpringBootApplication and @EnableAutoConfiguration:

Availability : SpringBootApplication was introduced in version 1.2 ,  While EnableAutoConfiguration was introduced in version 1.0

Purpose : @EnableAutoConfiguration enables auto configuration feature of Spring Boot application  which automatically configures things if certain classes are present in classpath e.g. : it can configure Thymeleaf  TemplateResolver and ViewResolver if Thymeleaf is present in the classpath.

On the other hand , @SpringBootApplication does three things.

  • It allows us to run the main class as a jar with an embedded container [Web server Tomcat]. 
  • It enables java configuration.
  • It enables component scanning. 




That's all from this interview.
Hope this post helps everybody in their interviews.
Thanks for reading!!

CAP Theorem and external configuration in microservices

 Hi friends, In this post, I will explain about CAP Theorem and setting external configurations in microservices. Question 1: What is CAP Th...