There is a set of annotations defined by JAX-RS under the javax.ws.rs package that you can use to build RESTful web services. The following table defines of some important annotations:
To build a RESTful web service based on JAX-RS, perform the following steps:
- Define the remote server component.
- Implement the deployment descriptor configurations.
- Write the web component that is the default home page of the web service application.
- Write the client program to consume the web service.
- The first step is to define the remote server component for the RESTful web service, which is as follows:
package jaxrs;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/restws")
public class RESTServer {
// This method is invoked if the request is
// not for HTML or XML
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getServerResponse() {
return "REST Server plain text response";
}
// This method is invoked if XML content is requested
@GET
@Produces(MediaType.TEXT_XML)
public String getXMLServerResponse() {
return "<restws>REST Server xml response</restws>";
}
// This method is invoked if HTML content is requested
@GET
@Produces(MediaType.TEXT_HTML)
public String getHtmlServerResponse() {
return "<html> " + "<title>" +
"REST Server Response" + "</title>"
+ "<body><h1>" + "REST Server HTML response" + "</h1>
</body>" + "</html> ";
}
}
- Implement the deployment descriptor configurations. Once the remote server for the RESTful web service is defined, the web.xml deployment description should have a reference to this component through the provider package, as follows:
<?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">
<servlet>
<servlet-name>Jersey REST Web Service</servlet-name>
<servlet- class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>
jersey.config.server.provider.packages
</param-name>
<param-value>jaxrs</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Web Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
- Write the web component that is the default home page of the web service application; being a web application, the default welcome page is index.html and can be defined as follows:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>REST Web Service</title>
</head>
<body>
<a href="rest/restws">Invoke the REST Web Service</a>
</body>
</html>
- Write the client program to consume the web service; once the preceding application is up-and-running on 4444 as the jaxrsrestful project, the RESTful web service is started and made available for the client program to invoke. The following is a client program that can invoke a remote web service for different media-type responses:
package jaxrs;
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.UriBuilder;
import org.glassfish.jersey.client.ClientConfig;
public class RESTClient {
public static void main(String[] args) {
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(getBaseURI());
//Invoke the REST Web Service for
// different media type responses
System.out.println(target.path("rest")
.path("restws").request()
.accep t(MediaType.TEXT_PLAIN)
.get(String.class));
System.out.println(target.path("rest")
.path("restws").request()
.accept(MediaType.TEXT_XML)
.get(String.class));
System.out.println(target.path("rest")
.path("restws").request()
.accept(MediaType.TEXT_HTML).get(String.class));
}
private static URI getBaseURI() {
//server is deployed on 4444 port as the
// jaxrsrestful project
return UriBuilder.fromUri(
"http://localhost:4444/jaxrsrestful").build();
}
}