0% found this document useful (0 votes)
19 views3 pages

Jax RS Step by Step

The document outlines a simple RESTful web service using Jersey, including an HTML file with a link to the service, a web.xml configuration for the servlet, and a Hello.java class that defines three endpoints for different media types (plain text, XML, and HTML). Additionally, it includes a ClientTest.java class that demonstrates how to make requests to the service and print the responses. The service is configured to run on a local server at the specified URI.

Uploaded by

Suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Jax RS Step by Step

The document outlines a simple RESTful web service using Jersey, including an HTML file with a link to the service, a web.xml configuration for the servlet, and a Hello.java class that defines three endpoints for different media types (plain text, XML, and HTML). Additionally, it includes a ClientTest.java class that demonstrates how to make requests to the service and print the responses. The service is configured to run on a local server at the specified URI.

Uploaded by

Suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Ex: 1

Index.html

<a href="rest/hello">Click Here</a>

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">
<servlet>
<servlet-name>Jersey REST 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>com.oracle.rest</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>

Hello.java

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class Hello {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey Plain";
}
// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
}

// This method is called if HTML is request


@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey HTML" + "</h1></body>" + "</html> ";
}

ClientTest.java

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 ClientTest {


public static void main(String[] args) {
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(getBaseURI());

System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_PLAI
N).get(String.class));
System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_XML)
.get(String.class));

System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_HTM
L).get(String.class));
}

private static URI getBaseURI() {

return UriBuilder.fromUri("http://localhost:8088/restfuljersey").build();

You might also like