Jersey From Scratch vol. 1

The headline says it all. This time I try do something for the newcomers. And in a completely new form.

Jersey’s got its first vlog!

Reading can be annoying and the tech people, let’s face it, are often not really shining at writing meaningful texts. I decided to try something new and more visual, so that you don’t have to put in the effort and try to decode my written thoughts. Instead, you can listen to my poor English in the following video, that shows how to create and run your first application with Jersey and Grizzly.

It’s a first try, so please be moderate with your judgements. I see lot of things to work on in the future, but the time is, as always, limited.

What did he say?

Many of you know that feeling. Hearing your voice from the recording is an eye-opening experience. I was prepared for almost everything, but the reality exceeded my expectations :-) I could have written the errata list, but apparently, pasting the entire text of what I really wanted to say seems to be shorter. Plus, it may be help those, who don’t understand my English.

So here it goes. Thanks for watching!

Transcript

Hi! And welcome to the first video from the “Jersey From Scratch” series. I will try to show you how to start developing RESTful webservices in Java. It’s actually easier than you might thing.

Let’s start by explaining the basics. Jersey, the framework we are going to talk about here, is the reference implementation of JAX-RS version 2, which is a JSR specification, -namely the JSR-339.

It defines a set of Java APIs for creating scalable Web services built according to the Representational State Transfer architectural style, known as REST.

There are loads of resources on the Internet explaining the philosophy and the core elements of the RESTful archtitecture, so we won’t cover it in our video. Instead, if you are completely untouched with REST, I will point you to a wiki page. At this point, I will asssume you already know what REST can offer and you want to incorporate it in your application.

To make it easier for you to start with Jersey, we created some maven archetypes, that will get you up and running within a minute.

Open your console and type mvn archetype:generate

Maven will list all the archetypes available and let you filter the list. Type jersey-quickstart and press enter.

In the filtered list, we have the archetypes from the com.sun group, which belong to the older version of Jersey, which is not a subject of this tutorial.

Find the jersey-quickstart-grizzly2 archetype from the org.glassfish.jersey.archetypes group, select the appropriate number – in our case it’s 5 – and press enter.
A list of versions will appear, from which you want to select the latest one. In our case – 50, but the number will change with each new release.

Now we need to answer few more questions, such as the groupId, let’s go with com.example, an artifactId, I won’t be too creative and type simply myapplication
and a version. Maven offers me the default 1.0-SNAPSHOT, which I am fine with, so I just press enter.

Next, a java package needs to be defined, com.example sounds good to me. I’ll stick with that for now.

Final review – Y for yes and enter. You should see the BUILD SUCCESS message. And – voile, there’s a new folder called myapplication which contains your generated app. Let’s explore it a bit.

As you can see, we have new maven pom file and the classical app structure with separate folder for sources and tests.

Now, before we leave our console, we will try to build and run our application. Type mvn clean install, which will compile your app, take care of all the dependencies, package the compiled classes into a jar file and run the tests.

Forget about tests for a while, I will cover the Jersey Test framework in a separate tutorial one day. Now, Run the app with mvn exec:java. This will create and run the lightweight Grizzly server and expose your service. Try to access the URI suggested in the log.

Use your broser, or simply run curl from another console window.

What you are getting back is the application WADL, which stands for Web Application Description Language, a standardized XML telling you where your services are exposed, what parameters do they expect, what media type do they consume and produce, and so on.

You see, that there’s a resource located at the myresource URI under http://localhost:8080/myapp/

Try to access it, use your browser or curl again and here it goes:

Got it!

That’s a response from you first Jersey RESTful web service.

And that’s a point, where we stop for today.
Next time we will import the sources into an IDE and look what’s inside the generated files.
We will dive into the code and start tweaking it – to get basic understanding of the language constructs.

Thank you for watching and stay tuned.

Leave a Reply

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