Hello World RESTful web service tutorial



Similar documents
Configuring iplanet 6.0 Web Server For SSL and non-ssl Redirect

c. Write a JavaScript statement to print out as an alert box the value of the third Radio button (whether or not selected) in the second form.

JBoss SOAP Web Services User Guide. Version: M5

Module 13 Implementing Java EE Web Services with JAX-WS

WIRIS quizzes web services Getting started with PHP and Java

Building and Using Web Services With JDeveloper 11g

Sample HP OO Web Application

Forms, CGI Objectives. HTML forms. Form example. Form example...

BAPI. Business Application Programming Interface. Compiled by Y R Nagesh 1

2. Follow the installation directions and install the server on ccc

HTML Forms and CONTROLS

Hands on exercise for

Further web design: HTML forms

Application Security

Working with forms in PHP

Developing Web Services with Apache CXF and Axis2

Contents. 2 Alfresco API Version 1.0

Yandex.Widgets Quick start

Web Services ( )

HTML Form Widgets. Review: HTML Forms. Review: CGI Programs

CREATE A CUSTOM THEME WEBSPHERE PORTAL

2- Forms and JavaScript Course: Developing web- based applica<ons

Web Service Development Using CXF. - Praveen Kumar Jayaram

LICENSE4J AUTO LICENSE GENERATION AND ACTIVATION SERVER USER GUIDE

Internet Technologies

Novell Identity Manager

Workshop for WebLogic introduces new tools in support of Java EE 5.0 standards. The support for Java EE5 includes the following technologies:

CI/CD Cheatsheet. Lars Fabian Tuchel Date: 18.March 2014 DOC:

Configuring IBM WebSphere Application Server 7.0 for Web Authentication with SAS 9.3 Web Applications

Implementing SQI via SOAP Web-Services

Getting Started with Web Applications

Creating Java EE Applications and Servlets with IntelliJ IDEA

Programming on the Web(CSC309F) Tutorial: Servlets && Tomcat TA:Wael Aboelsaadat

JWIG Yet Another Framework for Maintainable and Secure Web Applications

Getting Started. SAP HANA Cloud End-to-End-Development Scenarios. Develop your first End-to-End SAP HANA Cloud Application Scenario. Version 1.4.

Google App Engine f r o r J av a a v a (G ( AE A / E J / )

2.8. Session management

IDS 561 Big data analytics Assignment 1

CS412 Interactive Lab Creating a Simple Web Form

Intell-a-Keeper Reporting System Technical Programming Guide. Tracking your Bookings without going Nuts!

Operational Decision Manager Worklight Integration

Creating a RESTful Web Service for IMS-Transaction

SEEM4540 Open Systems for E-Commerce Lecture 06 Online Payment

Web Development 1 A4 Project Description Web Architecture

Overview of Web Services API

RPC over XML. Web services with Java. How to install it? Reference implementation. Setting the environment variables. Preparing the system

Web Applications. Originals of Slides and Source Code for Examples:

Appium mobile test automation

MASTERTAG DEVELOPER GUIDE

<option> eggs </option> <option> cheese </option> </select> </p> </form>

Deploying Intellicus Portal on IBM WebSphere

Outline. CS 112 Introduction to Programming. Recap: HTML/CSS/Javascript. Admin. Outline

Creating Web Services Applications with IntelliJ IDEA

CONFIGURATION AND APPLICATIONS DEPLOYMENT IN WEBSPHERE 6.1

Web Development and Core Java Lab Manual V th Semester

Step by step guides. Deploying your first web app to your FREE Azure Subscription with Visual Studio 2015

Website Login Integration

<Insert Picture Here>

the intro for RPG programmers Making mobile app development easier... of KrengelTech by Aaron Bartell

Consuming a Web Service(SOAP and RESTful) in Java. Cheat Sheet For Consuming Services in Java

Hadoop Tutorial. General Instructions

Multimedia im Netz Online Multimedia Winter semester 2015/16. Tutorial 03 Major Subject

Lab 1A. Create a simple Java application using JBuilder. Part 1: make the simplest Java application Hello World 1. Start Jbuilder. 2.

EJB 3.0 and IIOP.NET. Table of contents. Stefan Jäger / stefanjaeger@bluewin.ch

Oracle WebLogic Server

Implementing a Web Service Client using Java

Erie 1 BOCES/WNYRIC. Secure File Transfer. Upload/Download Wizard

ACCEPT THE SECURITY CERTIFICATE FOR THE WEB FILTER

Lab 5 Using Remote Worklight Server

JISIS and Web Technologies

Installing a Browser Security Certificate for PowerChute Business Edition Agent

CS Developing Web Applications with Java Technologies

Web Applications. For live Java training, please see training courses at

Configuring Tomcat for a Web Application

Your First Web Page. It all starts with an idea. Create an Azure Web App

TIBCO ActiveMatrix BPM Web Application Component Development. Software Release 2.0 November 2012

Web Programming II JSP (Java Server Pages) ASP request processing. The Problem. The Problem. Enterprise Application Development using J2EE

Specify the location of an HTML control stored in the application repository. See Using the XPath search method, page 2.

Using IIS and UltraDev Locally page 1

Creating your first Web service and Web application

Spectrum Technology Platform

Chapter 22 How to send and access other web sites

2X Cloud Portal v10.5

Struts Tools Tutorial. Version: M5

Introduction to XHTML. 2010, Robert K. Moniot 1

IBM Operational Decision Manager Version 8 Release 5. Getting Started with Business Rules

People Data and the Web Forms and CGI. HTML forms. A user interface to CGI applications

Accessing the Online Meeting Room (Blackboard Collaborate)

JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

Web services can convert your existing applications into web applications.

LAB 1. Familiarization of Rational Rose Environment And UML for small Java Application Development

Recommended readings. Lecture 11 - Securing Web. Applications. Security. Declarative Security

02267: Software Development of Web Services

Implementing the Shop with EJB

Java with Eclipse: Setup & Getting Started

Transcription:

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: 1

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: 2

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: 3

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> 4

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: 5

Click Next, and select the Create a simple project check-box: Click Next and type HelloRestClient for both Group Id and Artifact Id: 6

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"); 7

// 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: 8

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: 9