RESTful Web Services
REST fundamentals
- Everything in REST is considered as a resource.
- Every resource is identified by an URI.
- Uses uniform interfaces. Resources are handled using POST, GET, PUT, DELETE operations which are similar to Create, Read, update and Delete(CRUD) operations.
- Be stateless. Every request is an independent request. Each request from client to server must contain all the information necessary to understand the request.
- Communications are done via representations. E.g. XML, JSON.
You will need these jars:
Now create a new class UserInfo.java.
package com.restws;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("UserInfoService")
public class UserInfo {
@GET
@Path("/name/{i}")
@Produces(MediaType.TEXT_XML)
public String userName(@PathParam("i") String i){
String name = i;
return "" + "" + name + "" + "";
}
@GET
@Path("/age/{j}")
@Produces(MediaType.TEXT_XML)
public String userAge(@PathParam("j") int j){
int age = j;
return "" + "" + age + "" + "";
}
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("UserInfoService")
public class UserInfo {
@GET
@Path("/name/{i}")
@Produces(MediaType.TEXT_XML)
public String userName(@PathParam("i") String i){
String name = i;
return "" + "" + name + "" + "";
}
@GET
@Path("/age/{j}")
@Produces(MediaType.TEXT_XML)
public String userAge(@PathParam("j") int j){
int age = j;
return "" + "" + age + "" + "";
}
Now create web.xml file in WEb-INF folder.
Now create MyClient.java:
package com.restclient;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
public class MyClient {
public static final String BaseURI = "http://localhost:8080/RESTfulWS";
public static final String PATH_NAME = "/MyService/name/";
public static final String PATH_AGE = "/MyService/age/";
public static void main(String... args) {
String name = "Akash";
int age = 26;
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource resource = client.resource(BaseURI);
WebResource nameResource = resource.path("rest").path(PATH_NAME + name);
System.out.println("Client Response \n"
+ getClientResponse(nameResource));
System.out.println("Response \n" + getResponse(nameResource) + "\n\n");
WebResource ageResource = resource.path("rest").path(PATH_AGE + age);
System.out.println("Client Response \n"
+ getClientResponse(ageResource));
System.out.println("Response \n" + getResponse(ageResource));
}
/* Returns client response */
private static String getClientResponse(WebResource resource) {
return resource.accept(MediaType.TEXT_XML).get(ClientResponse.class)
.toString();
}
/* Returns the response as XML */
private static String getResponse(WebResource resource) {
return resource.accept(MediaType.TEXT_XML).get(String.class);
}
}
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
public class MyClient {
public static final String BaseURI = "http://localhost:8080/RESTfulWS";
public static final String PATH_NAME = "/MyService/name/";
public static final String PATH_AGE = "/MyService/age/";
public static void main(String... args) {
String name = "Akash";
int age = 26;
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource resource = client.resource(BaseURI);
WebResource nameResource = resource.path("rest").path(PATH_NAME + name);
System.out.println("Client Response \n"
+ getClientResponse(nameResource));
System.out.println("Response \n" + getResponse(nameResource) + "\n\n");
WebResource ageResource = resource.path("rest").path(PATH_AGE + age);
System.out.println("Client Response \n"
+ getClientResponse(ageResource));
System.out.println("Response \n" + getResponse(ageResource));
}
/* Returns client response */
private static String getClientResponse(WebResource resource) {
return resource.accept(MediaType.TEXT_XML).get(ClientResponse.class)
.toString();
}
/* Returns the response as XML */
private static String getResponse(WebResource resource) {
return resource.accept(MediaType.TEXT_XML).get(String.class);
}
}
Run MyClient.java as Java applicaiton, you will get following output:
Client Response
GET http://localhost:8080/RESTfulWS/rest/MyService/name/Akash returned a response status of 200 OK
Response
Client Response
GET http://localhost:8080/RESTfulWS/rest/MyService/age/26 returned a response status of 200 OK
Response
No comments:
Post a Comment