简体   繁体   中英

REST and RESTful WS

I have worked on RESTful Web Services at a high level (not very exhaustively), coded few REST based web services.

To begin my journey in RESTful WS, I read online articles (wiki, online tutorials etc.) and I came to know that it is an architectural style specifying constraints and when applied to web service induce desirable properties, such as performance, scalability etc. Ok, this is the theory part of this concept.

A simple example using JAX-RS :

@Path("/simple")
public class SimpleRESTService {

    @GET
    @Path("/{param}")
    public Response getMsg(@PathParam("param") String message) {
        String output = "Jersey say : " + message;
        return Response.status(200).entity(output).build();
    }
}

My doubt is:

If REST is architectural style (and using those guidelines build RESTful web services), what does JAX-RS do? To what I understood, REST is just architectural style and not any specification, what does JAX-RS has to do with this? I am having difficulty in understanding the relation between these two entities.

Can anyone help me understand this in simple words.

Java API for RESTful Web Services (JAX-RS), is a set if APIs to developer REST service. JAX-RS is part of the Java EE6, and make developers to develop REST web application easily.

You can read some more here: http://www.mkyong.com/tutorials/jax-rs-tutorials/

REST is indeed an architectural style of building web services independent of application-layer protocols (it could be implemented over FTP or HTTP as well).

JAX-RS is just a standardized API of Java EE with which you can easily implement RESTful services in Java. It is a toolkit for a developer to lift the burden off his shoulders, and as part of Java EE, it is implemented in application servers such as WildFly out of the box.

But note that it is just as possible to create REST services in frameworks like Spring using the Spring Web MVC and Spring HATEOAS modules.

Edit:
Although REST is a well-defined architectural style, it is (as you mentioned) not a specification so there is a lot of foggy areas about it among developers.

In a nutshell, all it states is "Hey man, if you use the communication protocol (say HTTP) this way and this way, it will be very convenient for you to change and scale and it will be very straightforward for the clients of your service to talk to you".

JAX-RS gives you the ability to define service endpoints with dynamic parts (eg.: /api/jediknights/{id}, so that the id could be anything), makes it easier to respond to the client in the format of your (or your client's) desire, like XML or JSON. It provides convenient wrapper objects to wrap your response body along with status codes and headers and many other things of convenience.

The catch however is: it is still your responsibility as a developer to adhere to the principles stated by the REST style . Nothing is really enforced upon you by JAX-RS.

To be fair, you could even achieve a RESTful web service by only using the somewhat lower-level Servlet API. JAX-RS just hides the unnecessary details from you and will make your life easier.

I hope it became a bit clearer :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM