Today I read about Restful services. Basically what I understand that is Restful webservices will work on HTTP request methods rather than normal webservice will work on SOAP request.
What is the need for Restful services as normal servlet can also work on the HTTP methods?
Servlets are API which is simple and provides capabilities to write server side components. Rest provides higher level support for writing services which are denoted by using the concept of resource which represents a concept to be addressed while representation indicates the current state of resource.
In Java Servlet RESTFul client tutorial, we create a RESTFul client in a Java Servlet with JAX-RS. Java API for RESTful Web Services (JAX-RS) is a Java API specification that provides support in creating web services according to the Representational State Transfer (REST) architectural pattern.
In a similar way, you can create REST applications using only the Servlet API. However, there are other APIs that are designed to create REST applications.
Servlets can run in Servlet container only but RESTful services can run in web container as well. RESTful talks about Resources/Entities/Verbs and gives you more Specific way to use the services i.e There is clear explanation on the usage of HTTP verbs. Request handling in both is totally different e.g. Servlets are multi-thr
The current chapter introduces a tried-and-true way to do RESTful services in Java: the service is implemented as a JSP script, which a web server such as Tomcat or Jetty translates into a servlet, and the servlet then is published with the web server.
The main player is the Servlet: A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol. If you want a very simplistic analogy, the Servlet is Java's version of CGI (Common Gateway Interface).
"Creating REST-service that will be used only by one Application isn't good. REST must be created only when it will be used by many apps. And REST has some disadvantages over servlet: REST is slower than servlet; it more difficult to write thread-safe REST than servlet".
RESTful is more an architecture style than a different technology. In server perspective, it is designed to be entirely stateless and self-contained on a per-request basis (i.e. there are no sessions). In client perspective, it's more a way of getting information in different formats via URLs with (self-documenting) path parameters instead of request parameters.
Surely you can do this with a plain vanilla servlet, but it would introduce some boilerplate code to gather the path parameters and to generate the desired response. JAX-RS is just a convenient and self-containing API which removes the need for writing all the boilerplate code yourself, resulting in minimal and more self-documenting code.
Assuming that you've a JAXB entity as model as below:
@XmlRootElement public class Data { @XmlElement private Long id; @XmlElement private String value; // ... @Override public String toString() { return String.format("Data[id=%d,value=%s]", id, value); } }
And a JAX-RS resource as below:
@Path("data") public class DataResource { @EJB private DataService service; @GET @Path("text/{id}") @Produces(MediaType.TEXT_PLAIN) public String getAsText(@PathParam("id") Long id) { return String.valueOf(service.find(id)); } @GET @Path("xml/{id}") @Produces(MediaType.APPLICATION_XML) public Data getAsXml(@PathParam("id") Long id) { return service.find(id); } @GET @Path("json/{id}") @Produces(MediaType.APPLICATION_JSON) public Data getAsJson(@PathParam("id") Long id) { return service.find(id); } }
Then you'd already get the desired content in proper format by:
That's it. Try to do the same with a single plain vanilla Servlet :) Please note that SOAP essentially also goes over HTTP. It's basically an extra XML layer over HTTP, not a different network protocol.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With