Re-requisites:
- Java JDK 1.8
- Spring Tool Suite IDE
- Internet Connectivity
This Article describes how to creating a new Spring-boot
project and making a simple Hello World program with Spring Tool Suite. Spring
Tool suite is a great IDE for developing spring based applications. It is
developed by Pivotal (Spring) based on Eclipse. For creating a fresh Spring
Boot project, there are several ways.
- Via -> Spring Initializr (https://start.spring.io/)
- Via-> STS -> Spring Starter Project
- Via -> STS -> Import Spring getting started contents
You are able to do it by selecting any of above options. For
this demonstration, I select the second option “Spring starter project”. No
matter you can do it, selecting any of above options. Let’s go.
Open your STS and go to this path.
File ->> New ->> Spring Starter Project.
After that, You will find a window like below.
Screen1
Then, You should provide the details for filling the form.
My project details are mentioned in Screen1. Type should be Maven. Because, We
are going to build a maven based project. After filling, go next. Then you can
select/change the spring boot version. You should select dependencies from
here. For this simple demonstration, lets select ‘Spring Web’. This dependency
is used for developing restful web services.
Screen2
And also, You can add/remove/modify dependencies after
creating the project on pom.xml file. In Maven, pom.xml handles project
dependencies. Lets click ‘next’ and
click ‘finish’. Now You can see, The created project in left side of your STS
IDE.
My 'Pom.xml' file contents are mentioned below.
pom.xml
<?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
https://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.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from
repository -->
</parent>
<groupId>com.claps</groupId>
<artifactId>MyTestApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MyTestApp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
If you do any change in pom.xml, update the maven project.
The way is, right click on the project -> Maven ->
Update Project.
Now I am going to implement the Hello World Program. There
are many ways to express this. I am going to do it using a controller class.
I created a controller main class ‘MyController’. To the
MyController, I set the annotation ->
@RestController
Then, Lets write a method for displaying “Hello world”.
Refer the following code.
MyController.Java
package
com.claps;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RestController;
@RestController
public
class MyController {
//TestAPI
@RequestMapping("/Hello")
public String hello() {
return "Hello world";
}
}
In the above my code, @RequestMapping annotation explains
how should be the URl end (context path). In this example it should be http://host:port/Hello
Below codes is the main Java file of my project. It creates
automatically when creating the project.
MyTestApplication.Java
package
com.claps;
import
org.springframework.boot.SpringApplication;
import
org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public
class MyTestAppApplication {
public static void main(String[] args) {
SpringApplication.run(MyTestAppApplication.class,
args);
}
}
Run the Program:
Screen3
Now, We have completed the work. Lets run the program
Right click on the project -> Run as -> Spring Boot
App
Then The Program will start..
Screen4
According to terminal screen, the allocated port is 8080.
Then My testing URI should be :
Now It can be test in your web browser. Type the URI on the
address bar and hit. Them You can see the response. Happy Coding !





No comments:
Post a Comment