Posts

Showing posts from February, 2012

RESTFul Servlets

Image
I already wrote the blog on RESTFull Dynamic service proxy ( have a ). In this blog, address the use of Servlets, because Servlets are best candidates for RESTFul services. The main advantage is HTTPServlet which cover the HTTP communication completely. RESTFul (HTTP method) HTTPServlet GET doGet() POST doPost() PUT doPut() DELETE doDelete() In this blog, I just use doPost() to store Person object in the server side Map (employees) and get the complete list of people (employees) as a response. All of the implementation from the RESTFull Dynamic service proxy blog, except the PersonService, which is replaced by the PersonServlet.java as shown in the following listing. package com.blogspot.ojitha.wsex7.web; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet...

RESTFull Dynamic service proxy

Image
A RESTFull provider implements the method public Source invoke(Source request) in the server side and the twin Dispatch object in the client side.  This way of handling the RESTFull is not easy. The best way is to use of JAX-RS as shown in my previous blog entries JAX-RS: 1 min RESTFul with JAXB   and  JAX-RS Tutorial: 1 min RESTFul service .   As shown in the above figure, The person is JavaBean with JAXB xml root as shown in the following listing. As well it compose an Address object for each and every Person. package com.blogspot.ojitha.wsex6.person; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; import com.blogspot.ojitha.wsex6.common.Address; @XmlRootElement @XmlType(propOrder={"id", "firstName", "lastName", "age","address"}, namespace="com.blogspot.ojitha.wsex6.person") public class Person { private String firstName; private String lastN...

JAX-RS: 1 min RESTFul with JAXB

Image
This is second post of the JAX-RS Tutorial: 1 min RESTFul service post extended to use JAXB. Follow the first tutorial to understand the Grizzly framework usage. I’ve created two classes Person and the Address as shown in the above class diagram. package com.blogspot.ojitha.rest.example.first; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Person { private String firstName; private String lastName; private Long id; private Address address; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; ...

JAX-RS Tutorial: 1 min RESTFul service

This is not intended to explain the REST, but one of the best ways to create simplest web service in JAX-RS 311. However, you need to install maven as a prerequisite. Eclipse is the IDE for me. mvn archetype:generate -DarchetypeCatalog=http://download.java.net/maven/2 This will prompt you for option to create project with different configurations. Select option 1, that is the simplest. If you are suppose to go with Eclipse, then don’t forget to execute the following command, that is mvn eclipse:eclipse Then you can import this project as an existing project in the Eclipse. In the service, important to provide the path in @path annotation. HelloWorldResource is the simplest web service. package com.blogspot.ojitha.rest.example.first; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("/hello") public class HelloWorldResource { @GET @Produces("plain/text") private String hello(){ return "Hello World...

JAX-WS Binary Data passing: using MTOM

Image
The preferred way of binary data passing in the JAX-WS is MTOM. In the previous blog ( JAX-WS Binary Data Passing: using base64binary ), binary data has been passed in the body of the SOAP. But in the MTOM, binary data is passed as a attachment. See the web service (which is modified from the previous blog). package org.ojitha.wsex4; import java.awt.Image; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.jws.WebMethod; import javax.jws.WebService; import javax.xml.ws.Endpoint; import javax.xml.ws.soap.MTOM; import javax.xml.ws.soap.SOAPBinding; @WebService(serviceName="ImageService", wsdlLocation="org/ojitha/wsex4/ImageService.wsdl") @MTOM public class ImageService { @WebMethod public Image getImage(String imgName) throws IOException{ System.out.println("Image name "+imgName); return ImageIO.read(new File(imgName)); } public static void main(String[] args){ Endpoint endpoint = Endpoint.create(n...

JAX-WS Binary Data Passing: using base64binary

Instead of passing binary data as an attachment to web services using either SOAP with Attachment or MTOM, in this blog, I use base64binary to pass in the SOAP body. For the MTOM (preferred way of sending binary data) see the next blog JAX-WS Binary Data passing: using MTOM . Web service as follows: package org.ojitha.wsex4; import java.awt.Image; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.jws.WebMethod; import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebService(serviceName="ImageService") public class ImageService { @WebMethod public Image getImage(String imgName) throws IOException{ return ImageIO.read(new File(imgName)); } public static void main(String[] args){ Endpoint.publish("http://127.0.0.1:9876/image", new ImageService()); } } The client program is, package org.ojitha.wsex4.client; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.i...