Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Introduction to RESTful Web Services—A JAX-RS Specification

DZone's Guide to

Introduction to RESTful Web Services—A JAX-RS Specification

Learn about web services, what they are, what they're used for, different types of web services, and their advantages over other specifications.

· Integration Zone
Free Resource

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

In this article, we will learn about the different types of web services. Further, we will concentrate on RESTful web services.

Web Service

An inter-operable service hosted & up-running over the web which could be accessed by different independent clients like web browser.

Types of Web Services

  • REST (Representational State Transfer)
  • SOAP (Simple Object Access Protocol)

REST 

  • JAX-RS 2.0 specification supports implementation of RESTful web services.

  • Different vendor implementation for this type are:

    • Apache CXF

    • JBoss RestEasy

    • Oracle Jersey (Prior 2.x)

    • Glassfish Jersey (2.x)

SOAP

  • JAX-WS 2.0 specification supports implementation of SOAP-based web services.
  • Different vendor implementation for this type are:
    1. JAX-WS viz., Top-down and Bottom-up approach
    2. Apache Axis2

Image title

Source: BenchResources.net team

In this particular article, we will discuss and learn JAX-RS 2.0 specification based Restful web service. Later in the upcoming articles, we will use Apache’s CXF (Celtix + XFire) to implement demo examples.

RESTful Web Services

In the REST of World, you call everything as resource and it is uniquely identified using URI i.e.; Uniform Resource Identifier. REST server and clients interacts using stateless HTTP protocol via four broadly classified HTTP verbs viz.

  • POST            - to CREATE/insert resource
  • GET              - to READ/select/retrieve internal resource
  • PUT              - to UPDATE/modify the resource
  • DELETE         - to DELETE/remove internal resource representation

Note: That forms CRUD

Relationships Between SQL and HTTP Verbs


Action



SQL


HTTP verbs


Create



Insert


POST


Read



Select


GET


Update



Update


PUT


Delete



Delete


DELETE


Formats Supported in RESTful Web Services

  • XML
  • JSON
  • TEXT
  • Image
  • HTML
  • PDF
  • etc

Advantages of RESTful Web Services

  • REST architectural pattern is basically lightweight in nature. So, when you have bandwidth constraints then prefer REST web service.
  • Easy and fast to develop.
  • Top sites like Twitter, Yahoo uses this pattern.
  • Most social networking sites like Facebook.com uses REST web services.
  • Mobile App development growing rapidly and for their server interaction, it uses this REST pattern as it is faster in processing request/response data.

JAX-RS Annotations

These are the most commonly used JAX-RS Annotations, when developing REST based web service. In the upcoming articles, we will understand & use these annotations and implement demo examples.

  • @Path (javax.ws.rs.Path)
  • @GET (javax.ws.rs.GET)
  • @POST (javax.ws.rs.POST)
  • @PUT (javax.ws.rs.PUT)
  • @DELETE (javax.ws.rs.DELETE)
  • @PathParam (javax.ws.rs.PathParam)
  • @QueryParam (javax.ws.rs.QueryParam)
  • @MatrixParam (javax.ws.rs.MatrixParam)
  • @FormParam (javax.ws.rs.FormParam)
  • @HeaderParam (javax.ws.rs.HeaderParam)
  • @Context (javax.ws.rs.core.Context)
  • @Consumes (javax.ws.rs.Consumes)
  • @Produces (javax.ws.rs.Produces)

@Path annotation

  • @Path specifies the relative URI path
  • It can be defined at class-level or method-level
  • Let understand by an example

Base URL for any web application is:

http://<server>:<port>/<context-root>

Append <url-pattern> tag from web.xml with Base URL that forms:

http://<server>:<port>/<context-root>/<url-pattern>

Append class-level @Path(“class-level”) annotation and then method-level @Path(“method-level”) annotation that forms:

http://<server>:<port>/<context-root>/<url-apptern>/<class-level>/<method-level>

Go through the examples in the upcoming articles to understand other annotations in detail

References

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

Topics:
web services ,rest ,integration

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}