Tutorial: Creating RESTful Web Services with EJB 3.1

RESTful web services has gained a lot of acceptance across the web, they are basically web services that follows the REST guidelines. In this short tutorial I’m going to create a very simple Hello World application that implements a RESTful Web Service using EJB 3.1.

I’m assuming that you already have your environment setup and also a Java EE project created and ready to use.
So let’s get started, the first step is to create a session bean that looks like this:

public class HelloWorld {
   public String sayHello() {
       return "Hello World!!";

That’s really a straightforward implementation, we’ve used the annotation @Path to specify the URI path we’re going to use to access this service, we also created an operation accessible via HTTP GET (indicated by the @GET annotation), and lastly we specified the media type that this operation will produce, in this case it’s a plain text, but it could also be a JSON, XML, etc.
After creating the session bean, the second and last step is to register the servlet adaptor in the web.xml:


That’s it! You have already created a RESTful Web Service!!
Now if you deploy you project, you can go to your browser and type http://{projectURL}/sayHello, you’ll see the string “Hello World!!” displayed on the page.

Hope this post was helpful for you,
Till next Time!

Recommended for you

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource (Reason: ... This is a very common error for people who are just getting started with Web Services, it's really simple to solve but sometimes developers spend hours struggling to find a solution. It happens when you create a web service and tries to access it from a different application, it won't work because y...
Example of ServletContextListener Implementation According to Oracle official documenentation, the ServletContextListener Interface is responsible for receiving notifications about the ServletContext lifecycle changes, more specifically its initialization and destruction. This can be really helpful for developers in some specific situations, like ...
Tutorial: Custom ListView with ImageViews and TextViews There is no doubt that the ListView is one of the most used Android UI components, in most cases developers find themselves in a situation where they need to customize this component, instead of just showing a list of Strings on the screen they also need to show more information, an ImageView, or ev...
React.js Tutorial: Consuming JavaEE RESTful Web Services In this post I'm going to show how to consume Web Services with React.js, more specificalyJavaEE RESTful Web Services. I've already written a post showing how to create these web services with EJB 3, if you haven't seen it yet just click on this link for more details, but if you're already familiar ...

One comment on “Tutorial: Creating RESTful Web Services with EJB 3.1

  1. immm

    thank you for this tutorial, do you have tutorial about how to make a restful web service between server/client using java, graphql, react

Leave a Reply

Your email address will not be published. Required fields are marked *

Obs: Use the tag <pre lang="LANGUAGE"> to include code blocks to your comment.
Example: <pre lang="javascript"> console.log('Test'); </pre>