June 01, 2018

Spring Boot Interview Questions And Answers

What is Spring Boot?
Spring Boot is not a framework, it is a way to ease to create stand-alone application with minimal or zero configurations.

With Spring Boot we can develop spring based application with very less configuration. It provides defaults for code and annotation configuration to quick start new spring projects within no time.


What are the advantages and disadvantages of using Spring Boot?
Advantages:
  • Reduce Development and Testing time, which increases productivity
  • Use of JavaConfig helps avoid usage of XML.
  • Avoid lots of maven imports and the various version conflicts.
  • It avoids writing lots of boilerplate Code, Annotations and XML Configuration.
  • Provide Opinionated Development approach.
  • Quick start to development by providing defaults.
  • It provides Embedded HTTP servers like Tomcat, Jetty etc. to develop and test our web applications very easily.
  • Requires less configuration-Since there is no web.xml file. Simply add classes annotated with@Configuration and then you can add methods annotated with@Bean, and Spring will automatically load up the object and manage it like it always has. You can even add @Autowired to the bean method to have Spring autowire in dependencies needed for the bean.
  • Environment Based Configuration-Using these properties, you can pass into the application which environment you are using with:-Dspring.profiles.active={enviornment}. Spring will then load up the subsequent application properties file at (application-{environment}.properties) after loading up the main application properties file.
Disadvantages:
It is bit difficult and time consuming process to convert existing or legacy Spring Framework projects into Spring Boot Applications.

Which build tool have you used to develop Spring Boot Application?

Spring Boot application can be developed using Maven as well as Gradle.

What is JavaConfig?

Spring JavaConfig is a product of the Spring community that provides a pure-Java approach to configuring the Spring IoC Container. It thus helps avoid using XML configurations.
The advantages of JavaConfig:
  • Object-oriented configuration: Because configurations are defined as classes in JavaConfig, users can take full advantage of object-oriented features in Java. One configuration class may subclass another, overriding its @Bean methods, etc.
  • Reduced or eliminated XML configuration: The benefits of externalized configuration based on the principles of dependency injection have been proven. However, many developers would prefer not to switch back and forth between XML and Java. JavaConfig provides developers with a pure-Java approach to configuring the Spring container that is conceptually similar to XML configuration. It is technically possible to configure the container using only JavaConfig configuration classes, however in practice many have found it ideal to mix-and-match JavaConfig with XML.
  • Type-safe and refactoring-friendly: JavaConfig provides a type-safe approach to configuring the Spring container. With generics, it is now possible to retrieve beans by type rather than by name, free of any casting or string-based lookups.
How to reload the changes on Spring Boot without having to restart server?
This can be achieved using Developer tools (Dev Tools). Once this dependency is configured, whenever any changes is made in the code, the embedded tomcat will restart. Thus, the dev tools, helps to improve the productivity of developers.

Dev tool will eliminates the need for manually deploying the changes every time. Spring Boot doesn’t have this feature when it has released it’s first version.
< dependency >
< groupId >org.springframework.boot< /groupId >
< artifactId >spring-boot-devtools< /artifactId >
< optional >true< /optional >
< /dependency >


What is Actuator in Spring Boot?
Actuator is one of the key feature in Spring Boot framework. It helps us to access the current state of the running application in production environment. There are several metrics that has to be checked and monitored in the production environment. Even some external applications may be using those services to trigger the alert message to concerned person. Actuator module exposes set of REST endpoints that can be directly accessed as a HTTP URL to check the status.

How to disable Actuator endpoint security in Spring Boot?
By default all sensitive HTTP endpoints are secured such that only users that have an ACTUATOR role may access them. Security is enforced using the standard HttpServletRequest.isUserInRole method.
We can disable security by using:
management.security.enabled=false

It is suggested to disable security only if the actuator endpoints are accessed behind firewall.

How to run Spring boot application to custom port?
We can specify the port in application.properties. e.g:
server.port=8090

What is YAML?
YAML is a human-readable data serialization language. It is commonly used for configuration files. Compared to properties file, YAML file is much more structured and less confusing in case we want to add complex properties in the configuration file.

How to implement security for Spring boot application?
spring-boot-starter-security dependency is used to implement security in Spring Boot. This requires very little code. The config class will have to extend WebSecurityConfigurerAdapter and override its methods.

If you are using Maven, then add below dependency in pom.xml:
 < dependency >
 < groupId > org.springframework.boot < /groupId >
 < artifactId > spring-boot-starter-security < /artifactId >
 < scope > test < /scope >
 < /dependency >


By adding the spring boot security starter dependency the basic security has already been configured by default. We can further customize the security configuration by writing our own authorization and authentication.

Let's create a new class SecurityConfig that extends the WebSecurityConfigurerAdapter and overrides its methods.

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("adminUser")
                .password("adminPass").roles("USER");
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/**").authorizeRequests().anyRequest().hasRole("USER")
                .and().formLogin().loginPage("/login.jsp")
                .failureUrl("/login.jsp?error=1").loginProcessingUrl("/login")
                .permitAll().and().logout()
                .logoutSuccessUrl("/welcome.html");

    }
}


Add a new page named login.jsp. Users will get redirected to this page for adding credentials.

Whenever user tries to open http://localhost:8080/welcome.html, the control will be automatically redirected to http://localhost:8080/login.jsp. When the user enters correct credentials, they will be able to visit all the other URLs correctly.

How to convert a simple Maven project into Spring Boot project?
We need to add 'spring-boot-starter-parent' in pom.xml. e.g:
< project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
    < modelVersion > 4.0.0< /modelVersion >
    < groupId > khs.springboot< /groupId >
    < artifactId > spring-api< /artifactId >
    < version > 0.0.1-SNAPSHOT< /version >
    < name > Spring Boot API< /name >

    < parent >
        < groupId > org.springframework.boot< /groupId >
        < artifactId > spring-boot-starter-parent< /artifactId >
        < version > 1.5.10.RELEASE< /version >
        < relativePath / >
    < /parent >

< /project > 

Adding below dependency, will add all the web related jars in your project:
 < dependency >
     < groupId > org.springframework.boot < /groupId >
     < artifactId > spring-boot-starter-web < /artifactId >
 < /dependency >  


To specify the Java version:
 < properties >
     < java.version > 1.8 < /java.version >
 < /properties >  < properties >
     < java.version > 1.8 < /java.version >
 < /properties > 


What is the use of @SpringBootApplication annotation in spring boot?
Earlier we need to annotate our Application class or Main class with quite a lot of annotations to start with e.g.
1). @Configuration to enable Java-based configuration,
2). @ComponentScan to enable component scanning,
3). @EnableAutoConfiguration to enable Spring Boot's auto-configuration feature, by adding beans based on class path settings, other beans, and various property settings.

but now you can do all that by just annotating your Application class with @SpringBootApplication, which is available from Spring 1.2 onwards.

We can say, the @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attribute.

@SpringBootApplication also marks the class as a BootStrap class which means you can run it as a normal Java class.

What are the parameters accepted in the @SpringBootApplication annotation?
excludeNames: Exclude the list of fully qualified class names from the auto configuration. This parameter added since spring boot 1.3.0.
scanBasePackageClasses: Provide the list of classes that has to be applied for the @ComponentScan.
scanBasePackages: Provide the list of packages that has to be applied for the @ComponentScan. This parameter added since spring boot 1.3.0.

How to run spring boot as a standalone application?
SpringApplication.run(YourMain.class, args);
  • This sets up default configuration.
  • Starts Spring application context.
  • Performs class path scan.
  • Start inbuild Tomcat server.

How to add a REST Controller in Spring Boot application?
A Controller is a Java class, which is marked with annotations. It contais the menthod, which will be called whenever user access the configured URL's.

@RestController, which is a stereotype annotation can be used with anyclass to make it a REST controller. The @RestController annotation informs to the Spring to render the result back to the caller.

What is the difference between spring @Controller and @RestController annotations in Spring Boot?
In Spring framework, a Controller is a class which is responsible for preparing a model Map with data to be displayed by the view as well as choosing the right view itself.

This Controller class can also directly write into response stream by using @ResponseBody annotation and complete the request. The behavior of writing directly into response stream is very useful for responding calls to RESTful web services, because their we just return data instead of returning a view.

Let us take an exmple to understand the difference between @Controller and @RestController annotations:

1).
@Controller
public class MyRESTClass{

  @RequestMapping(value={"/url"})
  @ResponseBody
  public ObjectResponse restFunction(){
      //...
      return instance
   }
}


2).
@RestController
public class MyRESTClass{

  @RequestMapping(value={"/url"})
  public ObjectResponse restFunction(){
      //...
      return instance
   }
}


When you use @RestController the @ResponseBody is activates by default, we don't need to add it explicitly. We can say, @RestController is composition of @Controller and @ResponseBody.

@RestController was added into Spring 4.0, which made the development of RESTful Web Services in Spring framework a bit easier, where as @Controller is an old annotation, exists since Spring started supporting annotation, officially it was added on Spring 2.5 version.

The job of @Controller is to create a Map of model object and find a view, but @RestController simply return the object and object data is directly written into HTTP response as JSON or XML.

The @Controller annotation indicates that the class is a "Controller" e.g. a web controller while @RestController annotation indicates that the class is a controller where @RequestMapping methods assume @ResponseBody semantics by default i.e. servicing REST API.

Once you mark a class @RestController then every method is written a domain object instead of a view.

What is the user of @RequestMapping annotation?
The @RequestMapping annotation is used to provide routing information. It tells to the Spring that any HTTP request should map to the corresponding method. We need to import org.springframework.web.annotation package in our file. e.g:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
   
    @RequestMapping("/greet")
    public String greet()
    {
        return "Hello! User";
    }
}


Open 'http://localhost:8080/greet' in the browser, it will display "Hello! User".

How can we return Objects from a Controller?
Let us take an example to understand this. Supposse we are creating a REST web-service which returns list of all employees, instead of return a 'String', we will return  List < Employee > .

1). Create a POJO class:
public class Employee {
    private int employeeId;
    private String employeeName;
   
    public Employee()
    {
        super();
    }
    public Employee(int employeeId, String employeeName) {
        super();
        this.employeeId = employeeId;
        this.employeeName = employeeName;
    }
   
    //getters and setters
}


2). Create a EmployeeController, which has method getAllEmployees(). This method returns list of all the employees.

@RestController
public class EmployeeController {

    @RequestMapping("/getAllEmployees")
    public List < Employee >  getAllEmployees()
    {
        List < Employee >  employees=new ArrayList < Employee > ();
        employees.add(new Employee(001, "Lissey Barnett"));   
        employees.add(new Employee(002, "Himaanshu Shuklaa"));
        employees.add(new Employee(003, "Robert D'mello"));   
        return employees;
    }
}


Now, when you open http://localhost:8080/getAllEmployees in the browser, it will return below JSON:
[{"employeeId":1,"employeeName":"Lissey Barnett"},{"employeeId":2,"employeeName":"Himaanshu Shuklaa"},{"employeeId":3,"employeeName":"Robert D'mello"}]
The Spring Boot, has automatically converted List < Employee >  into a JSON String. The generated JSON has key names corresponding to the property names of the Employee class and values are the values of those properties.

What is the significance of @Service annotation?
It is used to create a business service. In Spring, business service is typically Singleton. When the application starts up, Spring container creates instance of the business service and keeps the instance in memory, so that others can use it.

To declare a particular class as a business service, we need to use a Strerotype annotation @Service.

1). Create a Pojo class:
public class Company {
    private int companyId;
    private String companyName;
   
    public Company()
    {
        super();
    }
    public Company(int companyId, String companyName) {
        super();
        this.companyId = companyId;
        this.companyName = companyName;
    }
   
    //getters and setters
}
2). Now we will create a service class:
import org.springframework.stereotype.Service;

@Service
public class CompanyService {
    private List companies=Arrays.asList(
    new Company(784, "Google"),
    new Company(785, "Microsoft"),
    new Company(786, "KHS Pvt. Ltd."));   
   
    public List getAllCompanies()
    {
        return companies;
    }
}

3). To use the business service, we need to declare a private variable of this class in our controller with @Autowired annotation. e.g:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class ServiceController {
    @Autowired
    private CompanyService companyService;
   
    @RequestMapping("/getAllCompanies")
    public List getAllCompanies()
    {
        return companyService.getAllCompanies();
    }
}

Now, when you hit http://localhost:8080/getAllCompanies from browser, it will show list of all the companies:
[{"companyId":784,"companyName":"Google"},{"companyId":785,"companyName":"Microsoft"},{"companyId":786,"companyName":"KHS Pvt. Ltd."}]

What is the use of @RequestParam and @PathVariable in Spring Boot?
Both @RequestParam and @PathVariable annotations are used for accessing the values from the request. The key difference between @RequestParam and @PathVariable is that, @RequestParam used for accessing the values of the query parameters, where as @PathVariable used for accessing the values from the URI template.

Suppose you want to get a single company, then first you need to define a method to get the company details from id in CompanyService class.

public Company getCompanyById(int id)
{
    return companies.stream().filter(company->company.getCompanyId()==id).findFirst().get();
}

Inside the ServiceController, let's define two methods.

@RequestMapping("/getAllCompanies/{id}")
public Company getCompanyById(@PathVariable int id)
{       
    return companyService.getCompanyById(id);
}
   
@RequestMapping("/getAllCompaniesFromId/{compId}")
public Company getCompanyFromId(@PathVariable("compId") int id)
{       
    return companyService.getCompanyById(id);
}

Now when you hit http://localhost:8080/getAllCompanies/786, it will print
{"companyId":786,"companyName":"KHS Pvt. Ltd."}

And when you hit http://localhost:8080/getAllCompaniesFromId/786, it will print
{"companyId":784,"companyName":"Google"}


What is the use of @RequestBody and @ResponseBody annotations in SpringBoot?
@RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object.

Spring automatically deserializes the JSON into a Java type assuming an appropriate one is specified. By default, the type we annotate with the @RequestBody annotation must correspond to the JSON sent from our client-side controller.

@ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object.

How can you create a new resource using POST in Spring Boot?
Let's say, we want add a new company. For this first we need to add a addCompany() method in CompanyService.
public void addCompany(Company company)
{
    companies.add(company);
}

Now define a method in ServiceController. Since we want to use POST, we need define method in @RequestMapping.
@RequestMapping(method=RequestMethod.POST, value="/getAllCompanies")
public void addCompany(@RequestBody Company company)
{
    companyService.addCompany(company);
}

Now, when you call http://localhost:8080/getAllCompanies/ from POSt method, by passing below input, it will add a company:
{"companyId":787,"companyName":"Funny Zone Limited"}

To verify whether, the 'Funny Zone Limited' is added in the list or not, hit http://localhost:8080/getAllCompanies/,
[{"companyId":784,"companyName":"Google"},{"companyId":785,"companyName":"Microsoft"},{"companyId":786,"companyName":"KHS Pvt. Ltd."},{"companyId":787,"companyName":"Funny Zone Limited"}]

How can we implement Update in Spring Boot?
Add a updateCompany() method in CompanyService class.
public void updateCompany(int id, Company company)
{
    for (int i=0; i<=companies.size(); i++)
    {
        Company existingCompany=companies.get(i);
        if(existingCompany.getCompanyId()==id)
        {
            companies.set(i, company);
            return;
        }
    }
}

Now add a method inside ServiceController class:
@RequestMapping(method=RequestMethod.PUT, value="/getAllCompanies/{id}")
public void updateCompany(@RequestBody Company company, @PathVariable int id)
{
    companyService.updateCompany(id, company);
}

When you call http://localhost:8080/getAllCompanies/784 by PUT method and pass below parameter,it will replace the details of Company with id 784.
{"companyId":784,"companyName":"Scrutiny PR Agency"}

To verify whether, company with id 784 is updated in the list or not, hit http://localhost:8080/getAllCompanies/,
[{"companyId":784,"companyName":"Scrutiny PR Agency"},{"companyId":785,"companyName":"Microsoft"},{"companyId":786,"companyName":"KHS Pvt. Ltd."}]

How can we implement Delete in Spring Boot?
Add deleteCompanyById() method inside CompanyService:
public void deleteCompanyById(int id)
{
    companies.removeIf(company->company.getCompanyId()==id);
}

Now, deleteCompanyById() methid inside ServiceController:
@RequestMapping(method=RequestMethod.DELETE, value="/getAllCompanies/{id}")
public void deleteCompanyById(@PathVariable int id)
{       
    companyService.deleteCompanyById(id);
}

When you call http://localhost:8080/getAllCompanies/ from DELETE method, by passing some id, it will delete the corresponding company from the list.

What are different HTTP methods available?
  • GET: It is used to retrieve information. GET requests must be safe and idempotent, meaning regardless of how many times it repeats with the same parameters, the results are the same. GET requests can also be partial or conditional.
  • POST: It is often used to create a new entity, but it can also be used to update an entity.
  • PUT: It is used to store an entity at a URI. PUT can create a new entity or update an existing one. A PUT request is idempotent. Idempotency is the main difference between the expectations of PUT versus a POST request. PUT replaces an existing entity. If only a subset of data elements are provided, the rest will be replaced with empty or null.
  • PATCH: It only update the specified fields of an entity at a URI. A PATCH request is neither safe nor idempotent. That's because a PATCH operation cannot ensure the entire resource has been updated.
  • DELETE: This request is used when the resource need to be removed; however, the resource does not have to be removed immediately. It could be an asynchronous or long-running request.
What is the difference between the HTTP put and post methods?
  • Although both PUT and POST methods can be used to perform create and update operation in REST WebServices, the main difference between these two HTTP method is Idempotency. Like GET,  PUT request is also idempotent in HTTP, which means it will produce the same results if executed once more multiple times. If you execute POST request multiple times, it will end up create that many orders, but when you execute PUT it will always produce the same result because of its idempotent.
  • In the context of REST WebService, POST is often used to create a new entity, where as PUT is often used to update an existing entity.
How to change Tomcat port in Spring Boot?
In Spring Boot, by default the embedded Tomcat runs on 8080. We can change it by adding server.port in the application.properties file.

/src/main/resources/application.properties add below line, it will set HTTP port to 8888:
server.port=8888

-K Himaanshu Shuklaa

No comments:

Post a Comment

RSSChomp Blog Directory