Quick Start: Building a RESTful Web Service by Spring Tool Suite

This is an alternative Quick Start tutorial of building a simple RESTful web service.  Instead of offering choices of tools, I have chosen the specific ones.

What to Build

Part 1: A web service that replies with a greeting; this part is primarily based on the guidance from spring.io
Part 2: A web service that replies with an imaginary product price quote; an addition to the original guide
Note: The replies, in JSON format, are from Tomcat

Required Tools
(1) Spring Tool Suite (version 4 in this tutorial)
(2) JDK 8 or above (version 13 in this tutorial)


PART 1: A web service that replies with a greeting

Step 1
Install Spring Tool Suite, and launch it.

Step 2
Fill out the New Spring Starter Project form to generate the starter codes.
To launch the form: File > New > Spring Starter Project
Fill out the two pages of the form as below.

Page 1:


Notice that Gradle type is chosen for this example.
Change project Name, Group, Artifact, and Package to yours.

Page 2:


In the "Available" text field, enter "Web" to search for the web-related components.
Select "Spring Web".

Optional: Point the mouse cursor to Spring Web to bring up the help dialog. See that what specific dependencies will be added to to the project.  One of them, apparently, is RESTful.

Step 3:

On the navigational panel, locate the Java file "RestServiceDemoApplication.java" or yourArtifactName.java

This file has the main method that is the starting point of the application.  Check that the code is generated like what is shown in the screen-shot.




Step 4:

Create the Greeting class



Step 5:

Create the GreetingController class



Step 6:

Run the application, that will include launching the Tomcat server.



Step 7

Launch a browser, and go to address: localhost:8080/greeting



Here is the web service received by the browser:

{"id":1,"content":"Hello, World!"}


PART 2: A web service that replies with an imaginary product price quote

Step 8

Create a simple Product class that simulates a product in reality. In this class, there are only three fields: name, description, and listedPrice.




Step 9

Create a simple PriceQuote class that simulates a price quote in reality. In this class, there are only four fields: clientName, product, discount, and date.  The product field is declared as a Product class created above.




Step 10

Create the PriceQuoteController class that will reply with a PriceQuote web service.




Step 11

Run the application. That will include launching the Tomcat server.

Step 12

Launch a browser, and go to address localhost:8080/quote




Here is the web service received by the browser:

{"clientName":"Joe","product":{"name":"Pixel #","description":"Cell Phone by Google","listedPrice":999.99},"discount":0.2,"date":"2020-02-28T03:10:23.023+0000"}

We can see that the Product object is nested in the PriceQuote object.



Comments

Popular posts from this blog

Finding Median by Stream

Factorial by Different Styles in Java

Reduction by Java Stream