SlideShare a Scribd company logo
1 of 9
Download to read offline
1
Hello World RESTful web service tutorial
Balázs Simon (sbalazs@iit.bme.hu), BME IIT, 2015
1 Introduction
This document describes how to create a Hello World RESTful web service in Eclipse using JAX-RS and
WildFly.
2 RESTful service
Create a new Dynamic Web Project in Eclipse called HelloRest. Make sure to select Generate web.xml
deployment descriptor at the end of the wizard. The web project should look like this:
2
The JAX-RS annotated classes will be handled by a special servlet. This servlet must be declared in the
web.xml file. Change the web.xml file so that it contains the following lines shown with yellow
background:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<display-name>HelloRest</display-name>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
Create a new interface called IHello and a class called Hello in the src folder under the package
hellorest:
3
The IHello.java should contain the following code:
package hellorest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
@Path("hello")
public interface IHello {
@GET
@Path("sayHello")
public String sayHello(@QueryParam("name") String name);
}
The Hello.java should contain the following code:
package hellorest;
public class Hello implements IHello {
@Override
public String sayHello(String name) {
return "Hello: "+name;
}
}
Deploy the application to the server. It should be deployed without errors:
To test the service, type the following URL into a browser (FireFox, Chrome, IE, etc.):
http://localhost:8080/HelloRest/rest/hello/sayHello?name=me
The response should be:
4
The URL has the following structure:
 http://localhost:8080 – the protocol, domain name and port number of the server
 /HelloRest – the name of the web application
 /rest – the mapping of the REST servlet in the web.xml file
 /hello – the @Path mapping of the IHello interface
 /sayHello – the @Path mapping of the sayHello operation
 ?name=me – the name and value passed as a @QueryParam
3 HTML client
Create an HTML file called HelloClient.html under the WebContent folder:
The contents of the file should be the following:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Hello REST client</title>
</head>
<body>
<form method="get" action="rest/hello/sayHello">
Type your name: <input type="text" name="name"/>
<input type="submit" value="Say hello"/>
</form>
</body>
</html>
5
Redeploy the application to the server, and open the following URL in a browser:
http://localhost:8080/HelloRest/HelloClient.html
This is where the HTML is accessible. The following page should be loaded in the browser:
Type something in the text field and click the Say hello button:
The result should be the following:
4 Java client using RESTeasy
RESTeasy is the JAX-RS implementation of the WildFly server. It also has a client library, which can be
used in console applications. This example will show how.
RESTeasy requires some additional dependencies, so the easiest way to create a client is to use Maven.
Create a new Maven Project in Eclipse called HelloRestClient:
6
Click Next, and select the Create a simple project check-box:
Click Next and type HelloRestClient for both Group Id and Artifact Id:
7
Click Finish, and the project should look like this:
Edit the pom.xml and add the following lines shown with yellow background:
<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>HelloRestClient</groupId>
<artifactId>HelloRestClient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.10.Final</version>
</dependency>
</dependencies>
</project>
4.1 Untyped client
Create a new class called UntypedHelloClient under the package hellorestclient with the following
content:
package hellorestclient;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
public class UntypedHelloClient {
public static void main(String[] args) {
try{
// Create a new RESTeasy client through the JAX-RS API:
Client client = ClientBuilder.newClient();
// The base URL of the service:
WebTarget target = client.target("http://localhost:8080/HelloRest/rest");
8
// Building the relative URL manually for the sayHello method:
WebTarget hello =
target.path("hello").path("sayHello").queryParam("name", "me");
// Get the response from the target URL:
Response response = hello.request().get();
// Read the result as a String:
String result = response.readEntity(String.class);
// Print the result to the standard output:
System.out.println(result);
// Close the connection:
response.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
The project should look like this:
Right click on the UntypedHelloClient.java and select Run As > Java Application. It should print the
following:
9
4.2 Typed client
Create an interface called IHello under the package hellorestclient with the following content:
package hellorestclient;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
@Path("hello")
public interface IHello {
@GET
@Path("sayHello")
public String sayHello(@QueryParam("name") String name);
}
Create a class called TypedHelloClient under the package hellorestclient with the following content:
package hellorestclient;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
public class TypedHelloClient {
public static void main(String[] args) {
try {
// Create a new RESTeasy client through the JAX-RS API:
Client client = ClientBuilder.newClient();
// The base URL of the service:
WebTarget target = client.target("http://localhost:8080/HelloRest/rest");
// Cast it to ResteasyWebTarget:
ResteasyWebTarget rtarget = (ResteasyWebTarget)target;
// Get a typed interface:
IHello hello = rtarget.proxy(IHello.class);
// Call the service as a normal Java object:
String result = hello.sayHello("me");
// Print the result:
System.out.println(result);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Right click on the TypedHelloClient.java and select Run As > Java Application. It should print the
following:

More Related Content

What's hot

Using schemas in parsing xml part 2
Using schemas in parsing xml part 2Using schemas in parsing xml part 2
Using schemas in parsing xml part 2Alex Fernandez
 
Jsp & Ajax
Jsp & AjaxJsp & Ajax
Jsp & AjaxAng Chen
 
Mule esb first http connector
Mule esb first http connectorMule esb first http connector
Mule esb first http connectorGermano Barba
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESYoga Raja
 
Java Servlet
Java ServletJava Servlet
Java ServletYoga Raja
 
Asp Net Advance Topics
Asp Net Advance TopicsAsp Net Advance Topics
Asp Net Advance TopicsAli Taki
 
ASP.Net Presentation Part1
ASP.Net Presentation Part1ASP.Net Presentation Part1
ASP.Net Presentation Part1Neeraj Mathur
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)Talha Ocakçı
 
Installation of Drupal on Windows XP with XAMPP
Installation of Drupal on Windows XP with XAMPPInstallation of Drupal on Windows XP with XAMPP
Installation of Drupal on Windows XP with XAMPPRupesh Kumar
 
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?DicodingEvent
 
Web Technologies - forms and actions
Web Technologies -  forms and actionsWeb Technologies -  forms and actions
Web Technologies - forms and actionsAren Zomorodian
 
ASP.NET 03 - Working With Web Server Controls
ASP.NET 03 - Working With Web Server ControlsASP.NET 03 - Working With Web Server Controls
ASP.NET 03 - Working With Web Server ControlsRandy Connolly
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technologyTanmoy Barman
 

What's hot (20)

Using schemas in parsing xml part 2
Using schemas in parsing xml part 2Using schemas in parsing xml part 2
Using schemas in parsing xml part 2
 
Jsp & Ajax
Jsp & AjaxJsp & Ajax
Jsp & Ajax
 
Mule esb first http connector
Mule esb first http connectorMule esb first http connector
Mule esb first http connector
 
JSP Error handling
JSP Error handlingJSP Error handling
JSP Error handling
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
 
Java Servlet
Java ServletJava Servlet
Java Servlet
 
Asp Net Advance Topics
Asp Net Advance TopicsAsp Net Advance Topics
Asp Net Advance Topics
 
ASP.Net Presentation Part1
ASP.Net Presentation Part1ASP.Net Presentation Part1
ASP.Net Presentation Part1
 
JSP
JSPJSP
JSP
 
jsp MySQL database connectivity
jsp MySQL database connectivityjsp MySQL database connectivity
jsp MySQL database connectivity
 
ASP.NET Lecture 2
ASP.NET Lecture 2ASP.NET Lecture 2
ASP.NET Lecture 2
 
Ajax
AjaxAjax
Ajax
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)
 
Soap Component
Soap ComponentSoap Component
Soap Component
 
Asp.net.
Asp.net.Asp.net.
Asp.net.
 
Installation of Drupal on Windows XP with XAMPP
Installation of Drupal on Windows XP with XAMPPInstallation of Drupal on Windows XP with XAMPP
Installation of Drupal on Windows XP with XAMPP
 
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
 
Web Technologies - forms and actions
Web Technologies -  forms and actionsWeb Technologies -  forms and actions
Web Technologies - forms and actions
 
ASP.NET 03 - Working With Web Server Controls
ASP.NET 03 - Working With Web Server ControlsASP.NET 03 - Working With Web Server Controls
ASP.NET 03 - Working With Web Server Controls
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 

Viewers also liked

Sun==big data analytics for health care
Sun==big data analytics for health careSun==big data analytics for health care
Sun==big data analytics for health careAravindharamanan S
 
Big data-analytics-2013-peer-research-report
Big data-analytics-2013-peer-research-reportBig data-analytics-2013-peer-research-report
Big data-analytics-2013-peer-research-reportAravindharamanan S
 
Android chapter18 c-internet-web-services
Android chapter18 c-internet-web-servicesAndroid chapter18 c-internet-web-services
Android chapter18 c-internet-web-servicesAravindharamanan S
 
Personalizing the web building effective recommender systems
Personalizing the web building effective recommender systemsPersonalizing the web building effective recommender systems
Personalizing the web building effective recommender systemsAravindharamanan S
 
Introduction to recommendation system
Introduction to recommendation systemIntroduction to recommendation system
Introduction to recommendation systemAravindharamanan S
 
Content based recommendation systems
Content based recommendation systemsContent based recommendation systems
Content based recommendation systemsAravindharamanan S
 
Android ui layouts ,cntls,webservices examples codes
Android ui layouts ,cntls,webservices examples codesAndroid ui layouts ,cntls,webservices examples codes
Android ui layouts ,cntls,webservices examples codesAravindharamanan S
 
Combining content based and collaborative filtering
Combining content based and collaborative filteringCombining content based and collaborative filtering
Combining content based and collaborative filteringAravindharamanan S
 
Database development connection steps
Database development connection stepsDatabase development connection steps
Database development connection stepsAravindharamanan S
 

Viewers also liked (15)

uae views on big data
  uae views on  big data  uae views on  big data
uae views on big data
 
Tomcat + other things
Tomcat + other thingsTomcat + other things
Tomcat + other things
 
Sun==big data analytics for health care
Sun==big data analytics for health careSun==big data analytics for health care
Sun==big data analytics for health care
 
Big data-analytics-2013-peer-research-report
Big data-analytics-2013-peer-research-reportBig data-analytics-2013-peer-research-report
Big data-analytics-2013-peer-research-report
 
Android chapter18 c-internet-web-services
Android chapter18 c-internet-web-servicesAndroid chapter18 c-internet-web-services
Android chapter18 c-internet-web-services
 
Personalizing the web building effective recommender systems
Personalizing the web building effective recommender systemsPersonalizing the web building effective recommender systems
Personalizing the web building effective recommender systems
 
Chapter 2 research methodlogy
Chapter 2 research methodlogyChapter 2 research methodlogy
Chapter 2 research methodlogy
 
Cs548 s15 showcase_web_mining
Cs548 s15 showcase_web_miningCs548 s15 showcase_web_mining
Cs548 s15 showcase_web_mining
 
Introduction to recommendation system
Introduction to recommendation systemIntroduction to recommendation system
Introduction to recommendation system
 
Content based recommendation systems
Content based recommendation systemsContent based recommendation systems
Content based recommendation systems
 
Android ui layouts ,cntls,webservices examples codes
Android ui layouts ,cntls,webservices examples codesAndroid ui layouts ,cntls,webservices examples codes
Android ui layouts ,cntls,webservices examples codes
 
Combining content based and collaborative filtering
Combining content based and collaborative filteringCombining content based and collaborative filtering
Combining content based and collaborative filtering
 
Database development connection steps
Database development connection stepsDatabase development connection steps
Database development connection steps
 
Full xml
Full xmlFull xml
Full xml
 
Sql developer usermanual_en
Sql developer usermanual_enSql developer usermanual_en
Sql developer usermanual_en
 

Similar to Hello World RESTful Web Service Tutorial

Oracle Endeca Developer's Guide
Oracle Endeca Developer's GuideOracle Endeca Developer's Guide
Oracle Endeca Developer's GuideKeyur Shah
 
Understanding JSP -Servlets
Understanding JSP -ServletsUnderstanding JSP -Servlets
Understanding JSP -ServletsGagandeep Singh
 
Build Your First Java Jersey JAX-RS REST Web Service in less than 15 Minutes
Build Your First Java Jersey JAX-RS REST Web Service in less than 15 MinutesBuild Your First Java Jersey JAX-RS REST Web Service in less than 15 Minutes
Build Your First Java Jersey JAX-RS REST Web Service in less than 15 MinutesRobert Li
 
May 2010 - RestEasy
May 2010 - RestEasyMay 2010 - RestEasy
May 2010 - RestEasyJBug Italy
 
Laravel 8 export data as excel file with example
Laravel 8 export data as excel file with exampleLaravel 8 export data as excel file with example
Laravel 8 export data as excel file with exampleKaty Slemon
 
CTS Conference Web 2.0 Tutorial Part 2
CTS Conference Web 2.0 Tutorial Part 2CTS Conference Web 2.0 Tutorial Part 2
CTS Conference Web 2.0 Tutorial Part 2Geoffrey Fox
 
Web Server and how we can design app in C#
Web Server and how we can design app  in C#Web Server and how we can design app  in C#
Web Server and how we can design app in C#caohansnnuedu
 
Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Hugo Hamon
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web servicesnbuddharaju
 
Exploring Symfony's Code
Exploring Symfony's CodeExploring Symfony's Code
Exploring Symfony's CodeWildan Maulana
 
Python Code Camp for Professionals 1/4
Python Code Camp for Professionals 1/4Python Code Camp for Professionals 1/4
Python Code Camp for Professionals 1/4DEVCON
 
Developing RESTful WebServices using Jersey
Developing RESTful WebServices using JerseyDeveloping RESTful WebServices using Jersey
Developing RESTful WebServices using Jerseyb_kathir
 

Similar to Hello World RESTful Web Service Tutorial (20)

How to connect redis and mule esb using spring data redis module
How to connect redis and mule esb using spring data redis moduleHow to connect redis and mule esb using spring data redis module
How to connect redis and mule esb using spring data redis module
 
Oracle Endeca Developer's Guide
Oracle Endeca Developer's GuideOracle Endeca Developer's Guide
Oracle Endeca Developer's Guide
 
Understanding JSP -Servlets
Understanding JSP -ServletsUnderstanding JSP -Servlets
Understanding JSP -Servlets
 
Build Your First Java Jersey JAX-RS REST Web Service in less than 15 Minutes
Build Your First Java Jersey JAX-RS REST Web Service in less than 15 MinutesBuild Your First Java Jersey JAX-RS REST Web Service in less than 15 Minutes
Build Your First Java Jersey JAX-RS REST Web Service in less than 15 Minutes
 
May 2010 - RestEasy
May 2010 - RestEasyMay 2010 - RestEasy
May 2010 - RestEasy
 
Play 2.0
Play 2.0Play 2.0
Play 2.0
 
Laravel 8 export data as excel file with example
Laravel 8 export data as excel file with exampleLaravel 8 export data as excel file with example
Laravel 8 export data as excel file with example
 
CTS Conference Web 2.0 Tutorial Part 2
CTS Conference Web 2.0 Tutorial Part 2CTS Conference Web 2.0 Tutorial Part 2
CTS Conference Web 2.0 Tutorial Part 2
 
Web Server and how we can design app in C#
Web Server and how we can design app  in C#Web Server and how we can design app  in C#
Web Server and how we can design app in C#
 
Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2Build powerfull and smart web applications with Symfony2
Build powerfull and smart web applications with Symfony2
 
Asp.net
Asp.netAsp.net
Asp.net
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web services
 
Intro to Laravel 4
Intro to Laravel 4Intro to Laravel 4
Intro to Laravel 4
 
JavaServer Pages
JavaServer PagesJavaServer Pages
JavaServer Pages
 
Exploring Symfony's Code
Exploring Symfony's CodeExploring Symfony's Code
Exploring Symfony's Code
 
AJAX
AJAXAJAX
AJAX
 
AJAX
AJAXAJAX
AJAX
 
C# Unit5 Notes
C# Unit5 NotesC# Unit5 Notes
C# Unit5 Notes
 
Python Code Camp for Professionals 1/4
Python Code Camp for Professionals 1/4Python Code Camp for Professionals 1/4
Python Code Camp for Professionals 1/4
 
Developing RESTful WebServices using Jersey
Developing RESTful WebServices using JerseyDeveloping RESTful WebServices using Jersey
Developing RESTful WebServices using Jersey
 

Recently uploaded

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 

Recently uploaded (20)

Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 

Hello World RESTful Web Service Tutorial

  • 1. 1 Hello World RESTful web service tutorial Balázs Simon (sbalazs@iit.bme.hu), BME IIT, 2015 1 Introduction This document describes how to create a Hello World RESTful web service in Eclipse using JAX-RS and WildFly. 2 RESTful service Create a new Dynamic Web Project in Eclipse called HelloRest. Make sure to select Generate web.xml deployment descriptor at the end of the wizard. The web project should look like this:
  • 2. 2 The JAX-RS annotated classes will be handled by a special servlet. This servlet must be declared in the web.xml file. Change the web.xml file so that it contains the following lines shown with yellow background: <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <display-name>HelloRest</display-name> <servlet-mapping> <servlet-name>javax.ws.rs.core.Application</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app> Create a new interface called IHello and a class called Hello in the src folder under the package hellorest:
  • 3. 3 The IHello.java should contain the following code: package hellorest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.QueryParam; @Path("hello") public interface IHello { @GET @Path("sayHello") public String sayHello(@QueryParam("name") String name); } The Hello.java should contain the following code: package hellorest; public class Hello implements IHello { @Override public String sayHello(String name) { return "Hello: "+name; } } Deploy the application to the server. It should be deployed without errors: To test the service, type the following URL into a browser (FireFox, Chrome, IE, etc.): http://localhost:8080/HelloRest/rest/hello/sayHello?name=me The response should be:
  • 4. 4 The URL has the following structure:  http://localhost:8080 – the protocol, domain name and port number of the server  /HelloRest – the name of the web application  /rest – the mapping of the REST servlet in the web.xml file  /hello – the @Path mapping of the IHello interface  /sayHello – the @Path mapping of the sayHello operation  ?name=me – the name and value passed as a @QueryParam 3 HTML client Create an HTML file called HelloClient.html under the WebContent folder: The contents of the file should be the following: <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Hello REST client</title> </head> <body> <form method="get" action="rest/hello/sayHello"> Type your name: <input type="text" name="name"/> <input type="submit" value="Say hello"/> </form> </body> </html>
  • 5. 5 Redeploy the application to the server, and open the following URL in a browser: http://localhost:8080/HelloRest/HelloClient.html This is where the HTML is accessible. The following page should be loaded in the browser: Type something in the text field and click the Say hello button: The result should be the following: 4 Java client using RESTeasy RESTeasy is the JAX-RS implementation of the WildFly server. It also has a client library, which can be used in console applications. This example will show how. RESTeasy requires some additional dependencies, so the easiest way to create a client is to use Maven. Create a new Maven Project in Eclipse called HelloRestClient:
  • 6. 6 Click Next, and select the Create a simple project check-box: Click Next and type HelloRestClient for both Group Id and Artifact Id:
  • 7. 7 Click Finish, and the project should look like this: Edit the pom.xml and add the following lines shown with yellow background: <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>HelloRestClient</groupId> <artifactId>HelloRestClient</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-client</artifactId> <version>3.0.10.Final</version> </dependency> </dependencies> </project> 4.1 Untyped client Create a new class called UntypedHelloClient under the package hellorestclient with the following content: package hellorestclient; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.Response; public class UntypedHelloClient { public static void main(String[] args) { try{ // Create a new RESTeasy client through the JAX-RS API: Client client = ClientBuilder.newClient(); // The base URL of the service: WebTarget target = client.target("http://localhost:8080/HelloRest/rest");
  • 8. 8 // Building the relative URL manually for the sayHello method: WebTarget hello = target.path("hello").path("sayHello").queryParam("name", "me"); // Get the response from the target URL: Response response = hello.request().get(); // Read the result as a String: String result = response.readEntity(String.class); // Print the result to the standard output: System.out.println(result); // Close the connection: response.close(); } catch (Exception ex) { ex.printStackTrace(); } } } The project should look like this: Right click on the UntypedHelloClient.java and select Run As > Java Application. It should print the following:
  • 9. 9 4.2 Typed client Create an interface called IHello under the package hellorestclient with the following content: package hellorestclient; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.QueryParam; @Path("hello") public interface IHello { @GET @Path("sayHello") public String sayHello(@QueryParam("name") String name); } Create a class called TypedHelloClient under the package hellorestclient with the following content: package hellorestclient; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.WebTarget; import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; public class TypedHelloClient { public static void main(String[] args) { try { // Create a new RESTeasy client through the JAX-RS API: Client client = ClientBuilder.newClient(); // The base URL of the service: WebTarget target = client.target("http://localhost:8080/HelloRest/rest"); // Cast it to ResteasyWebTarget: ResteasyWebTarget rtarget = (ResteasyWebTarget)target; // Get a typed interface: IHello hello = rtarget.proxy(IHello.class); // Call the service as a normal Java object: String result = hello.sayHello("me"); // Print the result: System.out.println(result); } catch (Exception ex) { ex.printStackTrace(); } } } Right click on the TypedHelloClient.java and select Run As > Java Application. It should print the following: