Spring Boot Rest Service [Get/Post]

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:

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.

Any J2EE web application will have the following structure in Maven.

The Pom.xml should contain the dependencies for Spring Boot application., as below.

pom.xml contains following SpringBoot Dependencies & Plugin:

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)

SpringBootController.java (SpringBootController Class)

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.

Running the Spring Boot Application:

Once it is success, we have to start the spring-boot application with another maven command.

Once Spring Boot Application Started, you will get the logs.., as below, with Port number 8080 where Internal Tomcat server started and running.

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

URL: http://localhost:8080/courses/UG

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.

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.

Results returned to the client:

You can download the entire Maven SpringBoot source code here……..

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *