Build your first web application in Spring-boot

Reading Time: 2 minutes

Spring and Spring Boot:
Spring Framework could be a Java platform that has comprehensive infrastructure support
for developing Java applications. Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.

package com.knoldus.employee;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class EmployeeBenefitsApplication {

	public static void main(String[] args) {
		SpringApplication.run(EmployeeBenefitsApplication.class, args);
	}

}

If you use Gradle, add the following dependency to your build.gradle file:

implementation 'org.springframework.boot:spring-boot-starter-actuator'

If you use Maven, add the following dependency to your pom.xml file:
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Now run the service with curl (in a separate terminal window), by running the following command:

$ curl localhost:8080
Greetings from Spring Boot!



Spring framework: The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications – on any kind of deployment platform.A key element of Spring is infrastructural support at the application level


Data Access/Integration: The information Access/Integration layer consists of the JDBC,
ORM, OXM, JMS and dealings modules. The JDBC module provides a JDBCabstraction layer that removes the necessity to try to to tedious JDBC committal to writing and parsing of
database-vendor specific error codes. The ORM module provides integration layers
for standard object-relational mapping arthropod genus.

Create a Controller

In Spring’s approach to building web sites, HTTP requests are handled by a controller. You can easily identify the controller by the @Controller annotation.

package com.knoldus.employee.controller;

import com.knoldus.employee.model.Employee;
import com.knoldus.employee.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@PostMapping("/registration")
public String saveToken(@RequestBody Employee employee) {
String token = employeeService.saveToken(employee);
return token;
}

}




Spring Boot:
Spring Boot provides an honest platform for Java developers to develop a complete and
production-grade spring application that developers will simply run. Minimum configurations ar
required while not the necessity for a complete Spring configuration setup. Spring Boot is meant to
avoid advanced XML configuration in Spring and to develop a production prepared Spring
Applications in a neater method.

Advantages of Spring Boot
Spring Boot has many advantages, some of which are mentioned below:
• Resolving Dependency Conflict: Spring Boot helps in resolving dependency
conflict. It identifies required dependencies and import them for you.
• Compatible Version: Spring Boot as information of compatible version for all
dependencies. It minimises the runtime class-loader issues.
• Avoiding boilerplate code: Spring Boot’s “opinionated defaults configuration”
approach helps you in configuring most important pieces behind the scene. Override
them only when you need. Otherwise everything just works, perfectly. It helps in
avoiding boilerplate code, annotations and XML configurations.
• User Friendly: provides embedded HTTP server Tomcat so that you can develop and
test quickly.
• Integration with IDE: It has excellent integration with IDEs like eclipse and intelliJ
idea.

Disadvantages of Spring Boot

  • Lack of control. Spring Boot is used to create a lot of unused dependencies, resulting in a large deployment file;
  • The complexity and time-consuming process of converting a legacy or an existing Spring project to a Spring Boot application;
  • Not suitable for large-scale projects.

Knoldus-blog-footer-image

Written by 

Hey, Am Prem Shankar from Delhi, who likes to travel and explore more about nature.