Spring Boot Application is a stand-alone solution of a normal Spring Web Application.
It comes with pre-configured conventions of the spring settings.
It is very easy for a developer to create a Get/Post Rest Service using Spring Boot.,
Requirements:
JDK1.7+, Maven 3+
Stack:
Java. Spring Boot
Files going to created:
1 2 3 |
pom.xml MySpringBootWebApplication.java SpringBootController.java |
Like Every Basic Web Application, the Project structure needs to be proper when configuring in Maven, for a Spring Boot Application.
The very first step is to create basic structure of Web application.
Using Maven we can use the archetypes to create a empty web application., or from eclipse please create a web application using Maven.
When generating a web application, please provide the following groupID and artifactId.
1 2 |
groupId -my.tutorialflow.rest artifactId -SpringBootRestService |
Any J2EE web application will have the following structure in Maven.

The Pom.xml should contain the dependencies for Spring Boot application., as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?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> <groupId>my.tutorialflow.rest</groupId> <artifactId>SpringBootRestService</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> </parent> <name>SpringBootRestService</name> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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> |
pom.xml contains following SpringBoot Dependencies & Plugin:
1 2 3 |
org.springframework.boot.spring-boot-starter-web org.springframework.boot.spring-boot-maven-plugin org.springframework.boot.spring-boot-starter-test |
These dependencies automatically import all the libraries and jars which are required to run a spring boot web application.
All these libraries and jars required will be used dynamically during the startup of the web application.
The src folder should have the following three components in project structure for Maven to recognize this as a Web project.
main/java to store the java sources
main/resources to store the java resources.
main/webapp to store the jsp/html or web files.
Create a java classes in the following java sources location(package) if not generated, which is groupId, artifactId present in POM.xml.
By default if package is not generated, please provide the package name as groupID and artifactId as mentioned above during Maven creation.
MySpringBootWebApplication.java (Spring Boot Class)
1 2 3 4 5 6 7 8 9 10 11 12 |
package my.tutorialflow.rest.springbootrestservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MySpringBootWebApplication { public static void main(String[] args) throws Exception { SpringApplication.run(MySpringBootWebApplication.class, args); } } |
SpringBootController.java (SpringBootController Class)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
package my.tutorialflow.rest.springbootrestservice; import java.util.ArrayList; import java.util.List; import org.springframework.web.bind.annotation.*; @RestController public class SpringBootController { @RequestMapping("/") public String index() { return "Greetings from Spring Boot!"; } @GetMapping("/courses/{type}") public List getCoursesByTypeUsingGet(@PathVariable String type) { List list = new ArrayList(); if(type.equalsIgnoreCase("PG")){ list = getPGCourses(); } else if(type.equalsIgnoreCase("UG")){ list = getUGCourses(); } return list; } @PostMapping("/coursesByType") public List getCoursesByTypeUsingPost(@RequestParam String type) { List list = new ArrayList(); if(type.equalsIgnoreCase("PG")){ list = getPGCourses(); } else if(type.equalsIgnoreCase("UG")){ list = getUGCourses(); } return list; } /** Dummy Data Methods **/ public List getPGCourses() { List pgCourses = new ArrayList(); pgCourses.add("MCA"); pgCourses.add("MSc"); pgCourses.add("MTech"); return pgCourses; } public List getUGCourses() { List ugCourses = new ArrayList(); ugCourses.add("BCA"); ugCourses.add("BSc"); ugCourses.add("BE"); ugCourses.add("BTech"); ugCourses.add("BA"); return ugCourses; } } |
Clean & Build
Once configured, please execute the following instructions to clean & build the Spring Boot Application.
In the Project root directory, please execute the following command.
1 |
mvn clean install |
Running the Spring Boot Application:
Once it is success, we have to start the spring-boot application with another maven command.
1 |
mvn spring-boot:run |
Once Spring Boot Application Started, you will get the logs.., as below, with Port number 8080 where Internal Tomcat server started and running.
1 2 3 4 5 6 |
2019-01-03 17:11:48.719 INFO 13556 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2019-01-03 17:11:48.780 INFO 13556 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2019-01-03 17:11:48.785 INFO 13556 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2019-01-03 17:11:49.066 INFO 13556 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2019-01-03 17:11:49.160 INFO 13556 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-01-03 17:11:49.174 INFO 13556 --- [ main] m.t.r.s.MySpringBootWebApplication : Started MySpringBootWebApplication in 5.509 seconds (JVM running for 13.193) |
Now we can try the GET request URL in any browser, so that the result will be listed as below.
In Browser screen, we could see the following results of PG/UG listed, from our Spring Boot Application, based on the URL path.,
URL: http://localhost:8080/courses/PG
1 |
["MCA","MSc","MTech"] |
URL: http://localhost:8080/courses/UG
1 |
["BCA","BSc","BE","BTech","BA"] |
For accessing the POST Request, we need to try in any SOAP UI or any Rest Client application.
When calling the Post URL :http://localhost:8080/coursesByType
by setting the below request parameter and content type from the client.
1 |
Content-Type: application/x-www-form-urlencoded |
In the form request type, we need to add the request parameter as ‘PG’ or ‘UG’ for parameter name ‘type’ to get the corresponding results., as this is requested in our Controller class.
1 |
type=PG |
Results returned to the client:
1 2 3 4 5 6 7 |
[ "BCA", "BSc", "BE", "BTech", "BA" ] |
You can download the entire Maven SpringBoot source code here……..