RestFul Web
RestFul Web
==========================
Annotation Description
@PATH(your_path) Sets the path to base URL + /your_path. The base URL is based on
your application name, the servlet and the URL pattern from
the web.xml configuration file.
@POST Indicates that the following method will answer to an HTTP POST request.
@GET Indicates that the following method will answer to an HTTP GET request.
@PUT Indicates that the following method will answer to an HTTP PUT request.
@DELETE Indicates that the following method will answer to an HTTP DELETE
request.
@Produces(MediaType.TEXT_PLAIN[, more-types])
@Produces defines which MIME type is delivered by a method annotated with @GET. In
the example text ("text/plain") is produced. Other examples would be
"application/xml" or "application/json".
@Consumes(type[, more-types])
@Consumes defines which MIME type is consumed by this method.
@PathParam Used to inject values from the URL into a method parameter. This way
you inject, for example, the ID of a resource into the method to get the correct
object.
A RESTFul web services are based on HTTP methods and the concept of REST. A RESTFul
web service typically defines the base URI for the services, the supported MIME-
types (XML, text, JSON, user-defined, ...) and the set of operations (POST, GET,
PUT, DELETE) which are supported.
JAX-RS supports the creation of XML and JSON via the Java Architecture for XML
Binding (JAXB
2.2. Jersey
On the server side Jersey provides a servlet implementation which scans predefined
classes to identify RESTful resources. In your web.xml configuration file your
register this servlet for your web application.
http://your_domain:port/display-name/url-pattern/path_from_rest_class
SetUp:=
Download the Jersey distribution as zip file from the Jersey download site.
The zip contains the Jersey implementation JAR and its core dependencies. It does
not provide dependencies for third party JARs beyond those for JSON support and
JavaDoc.
Copy all JARs from your Jersey download into the WEB-INF/lib folder.
package com.vogella.jersey.first;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
// The class registers its methods for the HTTP GET request using the @GET
annotation.
// Using the @Produces annotation, it defines that it can deliver several MIME
types,
// text, XML and HTML.
Web.xml
========
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>com.vogella.jersey.first</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package.
-->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.vogella.jersey.first</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
====
You should be able to access your resources under the following URL:
http://localhost:8080/com.vogella.jersey.first/rest/hello
TutorialsTrainingConsultingProductsBooksCompanyDonateContact us
Search
Search
NOW Hiring
QUICK LINKS
Lars Vogel
Version 2.5
15.12.2015
In this tutorial Eclipse 4.5 (Mars), Java 1.8, Tomcat 6.0 and JAX-RS 2.0 (with
Jersey 2.11) is used.
Table of Contents
The PUT, GET, POST and DELETE methods are typical used in REST based architectures.
The following table gives an explanation of these operations.
GET defines a reading access of the resource without side-effects. The resource is
never changed via a GET request, e.g., the request has no side effects
(idempotent).
DELETE removes the resources. The operations are idempotent. They can get repeated
without leading to different results.
A RESTFul web services are based on HTTP methods and the concept of REST. A RESTFul
web service typically defines the base URI for the services, the supported MIME-
types (XML, text, JSON, user-defined, ...) and the set of operations (POST, GET,
PUT, DELETE) which are supported.
2.1. JAX-RS
Java defines REST support via the Java Specification Request (JSR) 311. This
specification is called JAX-RS (The Java API for RESTful Web Services). JAX-RS uses
annotations to define the REST relevance of Java classes.
2.2. Jersey
On the server side Jersey provides a servlet implementation which scans predefined
classes to identify RESTful resources. In your web.xml configuration file your
register this servlet for your web application.
http://your_domain:port/display-name/url-pattern/path_from_rest_class
This servlet analyzes the incoming HTTP request and selects the correct class and
method to respond to this request. This selection is based on annotations in the
class and methods.
A REST web application consists, therefore, out of data classes (resources) and
services. These two types are typically maintained in different packages as the
Jersey servlet will be instructed via the web.xml to scan certain packages for data
classes.
JAX-RS supports the creation of XML and JSON via the Java Architecture for XML
Binding (JAXB).
The most important annotations in JAX-RS are listed in the following table.
Annotation Description
@PATH(your_path) Sets the path to base URL + /your_path. The base URL is based on
your application name, the servlet and the URL pattern from the web.xml
configuration file.
@POST Indicates that the following method will answer to an HTTP POST request.
@GET Indicates that the following method will answer to an HTTP GET request.
@PUT Indicates that the following method will answer to an HTTP PUT request.
@DELETE Indicates that the following method will answer to an HTTP DELETE
request.
@Produces(MediaType.TEXT_PLAIN[, more-types]) @Produces defines which MIME type
is delivered by a method annotated with @GET. In the example text ("text/plain") is
produced. Other examples would be "application/xml" or "application/json".
@Consumes(type[, more-types]) @Consumes defines which MIME type is consumed by this
method.
@PathParam Used to inject values from the URL into a method parameter. This way
you inject, for example, the ID of a resource into the method to get the correct
object.
The complete path to a resource is based on the base URL and the @PATh annotation
in your class.
http://your_domain:port/display-name/url-pattern/path_from_rest_class
3. Installation of Jersey
Download the Jersey distribution as zip file from the Jersey download site.
The zip contains the Jersey implementation JAR and its core dependencies. It does
not provide dependencies for third party JARs beyond those for JSON support and
JavaDoc.
Copy all JARs from your Jersey download into the WEB-INF/lib folder.
TODO...
4. Web container
For this tutorial you can use any web container, for example Tomcat or the Google
App Engine.
If you want to use Tomcat as servlet container please see Eclipse WTP and Apache
Tomcat for instructions on how to install and use Eclipse WTP and Apache Tomcat.
Alternative you could also use the Google App Engine for running the server part of
the following REST examples. If you use the Google App Engine, you do not have to
install and configure Tomcat.
Tip If you are using GAE/J, you have to create App Engine projects instead of
Dynamic Web Project. The following description is based on Apache Tomcat.
5. Prerequisites
The following description assumes that you are familiar with creating web
applications in Eclipse. See Eclipse WTP development for an introduction into
creating web applications with Eclipse.
package com.vogella.jersey.first;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
// The class registers its methods for the HTTP GET request using the @GET
annotation.
// Using the @Produces annotation, it defines that it can deliver several MIME
types,
// text, XML and HTML.
}
This class register itself as a get resource via the @GET annotation. Via the
@Produces annotation it defines that it delivers the text and the HTML MIME types.
It also defines via the @Path annotation that its service is available under the
hello URL.
The browser will always request the HTML MIME type. To see the text version, you
can use tool like curl.
You need to register Jersey as the servlet dispatcher for REST requests. Open the
file web.xml and modify it to the following.
Run you web application in Eclipse. See Eclipse WTP for details on how to run
dynamic web applications.
You should be able to access your resources under the following URL:
http://localhost:8080/com.vogella.jersey.first/rest/hello
The browser requests the HTML representation of your resource. In the next chapter
we are going to write a client which will read the XML representation.
Jersey contains a REST client library which can be used for testing or to build a
real client in Java. The usage of this library is demonstrated in the following
tutorial.
Create a new Java project com.vogella.jersey.first.client and add the Jersey JARs
to the project and the project build path. Create the following test class.
package com.vogella.jersey.first;
import java.net.URI;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.client.ClientConfig;
String plainAnswer =
target.path("rest").path("hello").request().accept(MediaType.TEXT_PLAIN).get(String
.class);
String xmlAnswer =
target.path("rest").path("hello").request().accept(MediaType.TEXT_XML).get(String.c
lass);
String htmlAnswer=
target.path("rest").path("hello").request().accept(MediaType.TEXT_HTML).get(String.
class);
System.out.println(response);
System.out.println(plainAnswer);
System.out.println(xmlAnswer);
System.out.println(htmlAnswer);
}