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

Monday, August 5, 2019

Spring boot Example | Spring REST Example with Spring boot

In Spring Framework lots of modules like spring boot, spring web services, spring-security, spring Kafka, spring data, spring integration, spring cloud. etc. spring boot module used to create the spring-based web application.
Learn more about spring boot click on this link- Why We Used Spring boot? 

Spring boot Example

In this spring boot example provides,  how to create spring REST Application with spring boot using Maven project, spring REST, spring MVC, Devtools dependency, java 1.8, STS development tool, Spring Initializr, we are looking mainly used off the @Restcontroller, @Responsebody, @ComponentScan  annotation

Steps to Create the Springboot Application

1. Let's create the spring boot project using spring initializer,  Go to the start.spring.io and select the Maven project, fill the project name, package name, packaging jar/war, add the basic dependency like Web, Devtools, Actuator. and click on the generate project button. start the downloading. once complete the download, extract the zip folder.
Spring initializr, spring boot example
Spring Initializr
2. Open the Spring Tool Suite (STS), import the project as an existing maven project.
spring boot project, spring boot example
spring-boot project
3. Add the remaining spring-boot maven dependencies in pom.xml from the central maven repository

<?xml version="1.0" encoding="UTF-8"?>
<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>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.4.RELEASE</version>
  <relativePath /> <!-- lookup parent from repository -->
 </parent>
 <groupId>in.blogspot.scholars.java</groupId>
 <artifactId>springbootexample</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>springbootexample</name>
 <packaging>jar</packaging>
 <description>springbootexample project for Spring Boot</description>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <scope>runtime</scope>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
  <dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <scope>provided</scope>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat -->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
  </dependency>
  <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
  </dependency>
  <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
   <version>2.5</version>
   <scope>provided</scope>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>

</project>

4. Create an Employee bean class. i.e Employee.java

package in.blogspot.scholars.java.employee;

public class Employee {

private Integer id;
private String name;
private String dept;
private String address;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDept() {
return dept;
}

public void setDept(String dept) {
this.dept = dept;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", dept=" + dept + ", address=" + address + "]";
}
}

5. Create a service class that contains your business logic, that is EmployeeServiceImpl.java

package in.blogspot.scholars.java.employee;

import org.springframework.stereotype.Service;

@Service
public class EmployeeServiceImpl {

public Employee getEmployeeDetails() {

Employee employee = new Employee();
employee.setId(1);
employee.setName("Narendra");
employee.setDept("IT");
employee.setAddress("India");
return employee;
}
}

6. Create a Constant class for URL mapping. It is the best programming practice to define all constants separately. i.e UrlConstant.java

package in.blogspot.scholars.java.employee;

public class UrlConstant {

public static final String MESSAGE = "/message";
public static final String EMPLOYEE_URL = "/get-employee";

}


7. Create the controller that handles all the requests coming from the dispatcher servlet and prepare the model & view object and sending a response to the dispatcher servlet. i.e. EmployeeController.java
Dispatcher servlet is a parent controller that handles every request coming from the server and sending the response to the server.

package in.blogspot.scholars.java.employee;

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

@RestController
public class EmployeeController {

@Autowired
private EmployeeServiceImpl employeeServiceImpl;

@RequestMapping(value = UrlConstant.MESSAGE, method = RequestMethod.GET)
public String getMessage() {

return "welcome to the https://java-scholars.blogspot.com";
}

@RequestMapping(value = UrlConstant.EMPLOYEE_URL, method = RequestMethod.GET)
public Employee getEmployeeDetails() {

return employeeServiceImpl.getEmployeeDetails();
}
}

8. Finally, annotate the SpringbootexampleApplication.java class with @ComponentScan("base package name").

package in.blogspot.scholars.java.springbootexample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(value= {"in.blogspot.scholars.java.*"})
public class SpringbootexampleApplication {

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

}

9. How to Run a spring boot application?

Once complete the coding, configuration of the spring application.
Right-click on the project, select Run As and click on the Spring boot App or Java Application.

spring-boot app run

10. How to Test a spring application on any web browser?


Open any Web Browser, type the http://localhost:8080/get-employee
It's recommended the spring REST API testing purposed used the Postman or SOAPUI  tools.


here, localhost -for development stage or application running on the local system.
for production/testing/QA stages used the domain name or IP address instead of localhost. for example https://java-scholars.blogspot.in/get-employee or https://www.abc.com/get-employee

here, 8080 is the default port number of the Apache Tomcat server, if you want to change this port number then go to the application.properties and write the server.port=8081 or as per your choice

here, /get-employee is requested coming from the server to the controller, request handler check exact matching RequestMapping available or not, if available then sending the response. if Not available throw the  404 exceptions.

11. How to create an executable jar/war of the spring boot application? or maven command for jar/ war creation of the spring boot application.

Right-click on a project, select Run As  and click on a Maven clean,
Right-click on a project, select Run As and click on a Maven build, write the goals: package and click on Run button.

12. How to deploy spring boot application in Apache Tomcat server?

Created a jar/war location inside the target folder of your spring boot project.
When deploying the spring boot application on any server then pass the complete path of the jar/war file. Let's go to the command prompt for the deployment of jar/war.

C:\WINDOWS\system32>java -jar V:\target\springbootexample-0.0.1-SNAPSHOT.jar
Above command applicable for both packaging type(jar/war).

13. How to stop the server?

control + c

Spring boot related most important topics are-
What is spring boot?
Click here for download the project- springbootexample.zip

Friday, August 2, 2019

Learn Spring boot tutorial | Spring boot Introduction

spring boot, spring boot tutorialsIn spring boot tutorial, provides the details of the spring boot introduction, advantages of spring boot, spring boot examples, spring boot dependency and spring MVC application using spring boot example.

Spring boot Introduction

Spring-boot is a  module of spring framework provided by spring.io guys. Basically, spring boot is an open-source Java framework used to create the spring applications and microservices Spring-boot provides the core to the advanced concept to developed spring environment applications and projects.  Spring-boot  "just run" the spring web-based application as a standalone application.

Why We Used Spring boot?

When we create a spring MVC application without spring boot, in that No. of configuration metadata files, setup the server, create folder structure/project architecture, if small changes needed then required the un-deployments and deployments, data access configuration, etc.

When we create spring MVC application with spring boot, that minimizes the development time and maintenance, eliminating the lots of configuration files and server setup with the help of the application.properties, No need for deployment and redeployment using Devtools, etc

Advantages of Spring boot

  1. No need to set up the server, spring boot contains inbuilt apache tomcat and jetty servers.
  2. Just run your web-based spring application as a standalone or java application.
  3. It provides the inbuilt derby database to perform the data access layer.
  4. It minimizes the lots of setup and configuration files with the help of an application. properties
  5. We can easily implement microservices using spring boot.
  6. It reduced the code size, development times, maintenance of application and increased code reusability.
  7. Spring boot Actuator dependency, it is a tool to manage the production environment HTTP endpoint. 
  8. Spring boot Devtools dependency, its help and eliminate the deployment process on the server. (Changes any code then no need to undeployment and deployment).

Thursday, August 1, 2019

Advantages of Spring boot

  1. No need to set up the server, spring boot contains inbuilt apache tomcat and jetty servers.
  2. Just run your web-based spring application as a standalone or java application.
  3. It provides the inbuilt derby database to perform the data access layer.
  4. It minimizes the lots of setup and configuration files with the help of an application. properties
  5. We can easily implement microservices using spring boot.
  6. It reduced the code size, development times, maintenance of application and increased code reusability.
  7. Spring boot Actuator dependency, it is a tool to manage the production environment HTTP endpoint. 
  8. Spring boot Devtools dependency, its help and eliminate the deployment process on the server. (Changes any code then no need to undeployment and deployment).
Why we used spring boot?

We will analyze all the advantages of the spring boot with the help of spring boot example in the next chapter.