Developing RESTful Web Services with JAX-RS
Restful Web Services Quick Link at Oracle Tutorial is available. However, to simplify JAX-RS, I'm going to provide you important information and an example.The JAX-RS API uses Java programming language annotations to simplify the development of RESTful web services.
Actually, restful webservices JAXRS is better than JAXWS. In JAXRS you can integrate with another web application which may be created in PHP or .DOT net language's. It means that there is no dependencies on particular language's. In today world of computer science you can mix any language like server or client.
Let's Understand an example that there is a client who wants to execute server path and Server code.
package com.Dream.restClient;
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;
//JAX-RS Client Code
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("saveUser").request().accept(MediaType.TEXT_PLAIN).get(String.class));
}
private static URI getBaseURI()
{
return UriBuilder.fromUri("http://localhost:8080/Dream/").build();
}
}
Output :
Navneet Kaur Daughter's Name is Pia From BiharThe above client is getting path from this rest Application.
Root resource classes or a request method designator, such as
@GET
, @PUT
, @POST
, or @DELETE
.
will be used in the rest apllication classes.
package com.Dream.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("saveUser")
public class UserController {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String User()
{
//registered User at programmingshifts.com
Programmingshifts_User obj = new Programmingshifts_User(101,"Pia","Bihar");
return "Navneet Kaur Daughter's Name is "+obj.getName()+" From "+obj.getAddress();
}
}
class Programmingshifts_User
{
int id;
String name, address;
public Programmingshifts_User(int id, String name, String address) {
super();
this.id = id;
this.name = name;
this.address = address;
}
@Override
public String toString() {
return "Programmingshifts_User [id=" + id + ", name=" + name + ", address=" + address + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
This is the web.xml file in dynamic web project which contains servlet class information and mapping.
<?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.Dream.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>