0% found this document useful (0 votes)
24 views

Java Spring Boot - Building a RESTful Web Service

Quick Guide to start your first REST Web Service using Java Spring Boot framework.

Uploaded by

ac.mild
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Java Spring Boot - Building a RESTful Web Service

Quick Guide to start your first REST Web Service using Java Spring Boot framework.

Uploaded by

ac.mild
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Building a RESTful Web Service

With Java SpringBoot Framework in Microsoft Visual Studio Code


What is REST Web Service
REST stands for REpresentational State Transfer. REST is web standards based architecture and uses
HTTP Protocol. It revolves around resource where every component is a resource and a resource is
accessed by a common interface using HTTP standard methods. REST was first introduced by Roy
Fielding in 2000.

In REST architecture, a REST Server simply provides access to resources and REST client accesses and
modifies the resources. Here each resource is identified by URIs/ global IDs. REST uses various
representation to represent a resource like text, JSON, XML. JSON is the most popular one.

(source: https://www.tutorialspoint.com/restful/restful_introduction.htm)
Java SE Development Kit
(SDK)

Install the Java SDK. This will have to do


anything regarding your Java
Development projects.

Here’s the download page:


https://www.oracle.com/java/technologies/javase-downloads.html
Microsoft Visual Studio Code

Visual Studio Code is a capable Integrated


Development Environment (IDE) from
Microsoft that can handle a wide range of
programming language available today.

Here’s the download page:


https://code.visualstudio.com/Download
Spring Boot Extension Pack for
Visual Studio Code

Collection of extensions for


developing and deploying
Spring Boot Application.

Install this to initialize the


dependencies and auto
download it for your Java
projects.
Starting a new Java Spring Boot project

1 1. Press CTRL + SHIFT + P to show the VSCode Command bar. Type in “spring initializr” and select
the “Spring Initializr: Create Maven Project…“

2 2. Choose 2.4.5 for the version

3. Choose “Java” as the project language


3
4. Type in your project group name “com.example.rest”. Remember this as a reversed version of a web
domain name. You started with the 1st level domain and ends with subdomain as your application
4 category/type. Here’s the format example “<com_domain>.<company_name>.<app_category>”

5. Type in your app name “greeting” as the Artifact Id


5
6. Then choose “JAR” for your java application packaging type

6
7. Next, choose “8” for the Java version

7
Initiate your Dependencies

We’ll be creating a simple RESTful web service API, so type in


“web” to look for the related frameworks available, choose
“Spring Web”, and then press ENTER.

This Spring Boot framework will let your process HTTP


Request and send out HTTP Respond as the result of our
web service process.
Setting up your Application Package

Now you’ll choose the folder to put your Java Spring Boot template application
generated by the Initializr.

You can choose any folder in your local drive.

There will be a pop-up message in the lower-left corner of your VS Code screen
showing that the package generation process was successful.

Click “Open” on the prompt, to bring out the starting project we’ve just created
Start the Java Spring Boot project

You’ll be greeted with VS Code home screen


with the Project Explorer on the left side
showing the content of the newly created
Spring Boot project.

Before we start coding, if there’s another


pop-up telling you that this is a Java Project,
click “Yes” to proceed.
Sneak Peek of the Final Look
• Here’s the view of what we’ll try to accomplish. A web service that will return a JSon data when called
via HTTP Request.

• It will take an URI parameter called “nama”

• If nama provided, it will show greeting with the nama provided, if not then Dunia will be used as the
default nama

• The id will also increased automatically each time the service called

• http://localhost:8888/apakabar
{
"id": 1,
"isi": "Apa kabar, Dunia?!“
}

• http://localhost:8888/apakabar?nama=Manusia
{
"id": 2,
"isi": "Apa kabar, Manusia?!“
}
Start Coding
We’ll see on the Project Explore the path of our project, after “src\main\java”, is
following our group name and app name from before which is
“com\example\rest\greeting”.

Initilizr also auto build an java application class file using naming convention of
“<App_name>Application.java” like shown here.

• Click the java file to open.

• Also shown here, Initializr made the application class using the SpringBoot
dependencies that we’ve picked earlier.

• @SpringBootApplication is an annotation that adds all of the following:

@Configuration: Tags the class as a source of bean definitions for the


application context.
@EnableAutoConfiguration: Tells SpringBoot to start adding beans based on
“classpath” settings, other beans, and various property settings. For example, if
“spring-webmvc“ is on the “classpath”, this annotation flags the application as
a web application and activates key behaviors, such as setting up a
DispatcherServlet.
@ComponentScan: Tells Spring to look for other components, configurations, and
services in the “com/example/rest/greeting” package, letting it find the
controllers.
Start Coding

Now we start coding by creating the entity class to represent 1


our data, which are id and content.

1. First Right-Click on the “greeting“ folder then choose


“New Folder”, then name it “entity”.

2. Right-Click on the “entity“ folder then choose “New 2


File”, then name it “Salam.java”.
Start Coding

• VS Code will generate the template class along with the


application package used.

• Create 2 class properties, id with DataType of long,


content with DataType of String

• Create Get method for each of the properties above.

• Create a constructor method for Salam class which will


assign the value of 2 properties before.

• Save your work, and on to another class.


Start Coding
• Now we will prepare the Controller class that will handle the
HTTP GET Request using @RestController annotation from
SpringBoot framework and @GetMapping annotation to link the
“/salam” URI. We also use @RequestParam annotation to
handle the values of “nama” parameter from the HTTP Get
Request and use “Dunia” as its’ if none was entered.

• A new Salam object will be created as the result with id and


content attributes based on the values of counter and
templates properties, and it will auto converted to JSON by
Spring’s HTTP message Converter support

1. Right-Click on the “greeting“ folder then choose “New


Folder”, then name it “controller”, then Right-Click on
the “controller“ folder then choose “New File”, then name
it “SalamController.java”.

2. Continue by typing in the Class contents. Don’t worry of linking


the required library using import, VS Code will do it for you as
you typed in your code. Type in the codes on the right so you
can feel it.
It’s Time to Greet the World

• Return to the GreetingApplication.java file that auto


created by the Spring Boot Initializr.

• Click the Play button on the Top-Right corner of the


VS Code editor

• Chose Run Java to compile and run the RESTful Web


Service
Compile Error

• This result maybe different on each machine, but mine seem to have
port 8080 used by other service, which in turn will cause the compiler to
throw error like this:
Description:
Web server failed to start. Port 8080 was already in
use.
Action:
Identify and stop the process that's listening on
port 8080 or configure this application to listen on
another port.

• This can be rectify either by stopping the other service using the port or
by changing our own port designation. We choose the later because its’
more feasible in the real world scenario.
Compile Error

• To assign a new port for our web service, we must first


open “application.properties” file inside the
“src\main\resources“ folder, then type in this
configuration statement:

server.port:<new_port_number>

• Save it, then we could try to re-run the


GreetingApplication.java.
Greeting Re-Run

• If no more hurdles arrived, the re-run will be success


and our Web Service will be live inside the newly
assigned port as shown here it used 8888 port of
the Localhost HTTP from the default Tomcat
Web Server provided by the framework.

• Next we’ll be checking how it looks on the web browser


Greeting Re-Run

• Here’s what will be shown when Firefox Developer Edition attempting


to open the web service at:

http://localhost:8888/salam

• And here’s what it looked in the Raw Data format with Pretty Print activated

• As we create the web service to handle “nama” parameter, here’s what


would happen if we set the value as “Manusia”:

http://localhost:8888/salam?name=Manusia
More questions about SpringBoot?

Thank you for sticking with this tutorial until the end.

If you got any questions or something to discuss related to the subject,


please feel free to contact me at [email protected]

See you at the next course

You might also like