03: JAX-RS how to create a RESTFul Web Service in Java?

Q. What are the high level steps in developing a RESTful web service in Java?
A. You can see the complete tutorial in the tutorials section. This post is to quickly explain the major steps at the job interview. Knowing all the basics is of no use if you can’t write a web service.

Step 1: Create a Maven project structure:

Step 2: Need the libraries/frameworks

This example shows RESTEasy as JAX-RS implementation from JBoss.

Step 3: Web service interface with JAX-RS annotations

Folder: src/main/java

Step 4: Implementation of Web service interface

Step 4: Spring XML config as IoC.

Folder: src/main/resources/com/mytutorial

Step 5: web.xml deployment descriptor

to bootstrap Spring and initialize servlet, mapping, etc. Folder: src/main/webapp/WEB-INF.

Build the war and deploy to say JBoss application server.

Step 6: JBoss deployment descriptor

Folder: src/main/webapp/WEB-INF/jboss-web.xml

The URL = http://localhost:8080/tutorial/rest/myapp/name/arul

tutorial: defined in jboss-web.xml (application context)
rest: defined in web.xml (servlet-mapping)
myapp/name/arul: in SimpleRestWeb interface (@Path annotation at class and method level)

Project Structure:

RESTful Web Service using RESTEasy

RESTful Web Service using RESTEasy

Hmmm, some of the other steps are agreeing on content type like XML, JSON, etc among the producer & consumers, the contract for the content, service retry logic, service timeouts, using JAXB to map XML/JSON to Java objects, etc need to be sorted out.

300+ Java & Big Data Interview FAQs

16+ Java Key Areas Interview Q&As

800+ Java Interview Q&As

300+ Java & Big Data Tutorials