Spring Boot, create first application using Spring Boot

0


Spring Boot can also be used to develop enterprise applications but we mostly prefer spring boot for API Design.

spring boot is a utility project which aims to make it easy to build spring-based, production-ready applications and services with minimum fuss. it provides the shortest way to get a spring web application up and running with the smallest line of code/configuration out-of-the-box.

What is API?

API means an application programming interface that is used to communicate data from application to database server.

API uses HTTP Method to communicate data.

1)  HttpGet:-  to fetch all records from databases  

2)  HttpPost:-  to submit or post record to server from application

3)  HttpPut:-   to replace or edit the record to server from application

4)  HttpDelete:-  to delete record from the server from the application

5)  HttpPatch:-  to partially update the record 

6)  Connect:-  to connect server we can use connect

7)   header:-  to send only header content such as content type, size, and token

What type of result will be returned by API?

API will return JSON as a result, JSON means JavaScript Object Notation, It is used to provide a structured approach to output using key: value pair.

Syntax of JSON for Single row:-

{'key':value}

Syntax of JSON for Multiple rows

{

  [{key:value},{key:value}]

}

all table columns will be worked as a key and table data will work as a value.

spring boot requirements

setting up and running spring boot applications requires the following:

  • java 8+
  • spring framework 5.0.1.release or above
  • servlet 3.0+ container (in case you don’t make use of the embedded servers).

Create Simple Application using Spring Boot:-

1)   File  ---> New Maven Project  --->Nextr --> Next
2)  Modify pom.xml file only create dependencies 
    <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>scs</groupId>
  <artifactId>helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>
  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
3)  Add controller under package
package scs.welcomespring;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@EnableAutoConfiguration
public class WelcomeController {
@RequestMapping("/welcome")
@ResponseBody
public String sayWelcome() {
return "Welcome in Spring Boot Application";
}
}
4)   Create Main Class for Calling
package scs.welcomespring;
import org.springframework.boot.SpringApplication;
public class SpringBootMain {
public static void main(String[] args) {
SpringApplication.run(WelcomeController.class,args);
}
}
Now run the main class method and type the URL 
localhost:8080/welcome

4.1 create a maven web project

create a maven web project using this and name your project springbootrestservice.

make sure to use java 8+ since spring boot doesn't work with earlier versions.

4.2 configure pom.xml

the second step is to configure spring boot in pom.xml.

all spring boot applications extend from spring-boot-starter-parent, so before defining your dependencies, define the parent starter as follows:

now since we're creating a rest api, we're going to use spring-boot-starter-web as a dependency which would implicitly define all the required dependencies like spring-core, spring-web, spring-webmvc, servlet api, and Jackson-databind library, so just add the following as a dependency:

the next step is to add the spring boot plugin as follows:

then define the spring repositories:

the final step is to set the packaging property as a jar, so that maven generates a runnable jar file on the build.

here is the full pom.xml file:



4.3. create rest resources

now we're going to create our payment controller along with the request and response POJO classes

following is the payment request class that should be submitted by clients on each payment request:

and this is the base response returned back from our service:

and this is our controller:

4.4 create the application class

this final step is to create the configuration and starter class, spring boot supports a new annotation @springbootapplication which is equivalent to using @configuration, @enableautoconfiguration, and @componentscan with their default attributes.

so you just have to create a class annotated with @springbootapplication and spring boot will enable the auto-configuration and would scan for your resources in the current package:

5. deploy the spring boot application

now let's make use of the third awesome feature of spring boot which is the embedded server, all we need to do is to just generate a runnable jar file using maven and run it as a normal standalone application.

  • right click pom.xml -> run-as -> maven install
  • maven generates a runnable jar file called springbootrestservice-1.0.jar inside the target folder.
  • open cmd, then run the jar using: java -jar springbootrestservice-1.0.jar

here we go, our rest api is up and ready to serve requests at port 8080 by default.

in this tutorial, we introduced spring boot features and created a fully working example using the spring boot embedded server.

hope you like it, for clarifications please leave your thoughts in the comments section below.

Tags

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)