Am I headed in the right direction with REST and Java?

aL Mac

Gawd
Joined
Jul 20, 2002
Messages
949
Hey guys, I'm primarily a software developer for embedded systems and desktop applications. My primary language being C# .NET. As you can see, I'm not much of a web developer.

I'm working on a project for my master's degree that involves machine learning that is supposed to be a tool that a particular website can leverage to get a classification about some data.

Basically, I want a simple interface to my tool. The website operates with a LAMP stack. Here's what needs to happen.

  1. Website gathers information and create a particular object describing some data
  2. Website sends me the object and asks me for a classification
  3. I get the object, analyze it, return the classification (good or bad basically)
  4. The website decides what to do with my result

Basically, I just need the equivalent of:

classification = myTool.GetClassification(data);

but across a web service, where the data might be a more complex object with a lot of properties

It was suggested that I could do this with a simple REST web service. The libraries I need to interface with are written in Java and they run Linux web servers, therefore I'm using Java to implement the tool because it appears to be the best choice with my background and the environment they use.

I've been looking into setting up an Ubuntu virtual machine, running Tomcat, with a REST web service to handle the requests from the website (as my development environment). The website would send me some JSON describing the data, and I would respond with a JSON object with information about the result.

I understand the basics on how RESTful services work and map to HTTP requests, but I'm not familiar with JAX-RS implementations in Java and how it all works out. I thought, at first, that the website would basically be sending me a GET request with JSON as the body describing the data they wanted analyzed, and I would issue a response with JSON as the body describing the result. However, I've since learned that GET requests with a body are not really HTTP conforming. Instead, there are query strings and URLs for a GET request, but those are limited by maximum lengths which probably not work well in my case because I need to send a longer object to the tool (maybe quite a bit of text and many properties). I think that a REST interface that wanted to conform to HTTP standards would be left with only POST as an option.

So with a Java REST API like Jersey or RESTeasy, how does this all work? Do I just map an object in java to the API using special attributes in the code and it figures this out for me? Would it know to use a get or a post? Is this a reasonable way to move forward with what I'm doing?

I could spend quite some time trying to learn all about Java REST web services. While, I'd love to learn, I'm limited for time and I don't want to be barking up the wrong tree. If someone who was more knowledgeable in this area could give me some advice, I would really appreciate it.
 
To answer your question regarding HTTP methods and how they relate to REST, you are correct in your assumption that POST is really the only option for the submission of data.

If you are using an MVC framework, you would map an object in java using special attributes in the model code (class definition).

Then in the controller code, you would specify that object as an input parameter using special attributes, and you would also specify then which http method the controller function is listening on (POST/GET).

A quick google search turned up this tutorial which may be helpful: http://java.dzone.com/articles/resteasy-spring
 
So with a Java REST API like Jersey or RESTeasy, how does this all work?

It's been a while since I've written a RESTful web service with JAX-RS, so what I'm about to provide is a rough 'pseudo-example' if you will. There's additional work you'll have to do to get a working web service...This is merely to give you an idea of how these things are set up and what they'll look like.

RESTEasy uses an annotation library. You define a method in Java, and then give it a @GET, @POST, @PUT or @DELETE annotation to specify which type of HTTP method it will be mapped to. You then give it an @Path("/somepath") annotation to specify what URL it will be mapped to. If the method takes any parameters, you'll map those parameters from the request to the actual method arguments with more annotations.

So if you're writing a dog show API (oversimplified example, of course), and want to implement an HTTP Get method on the resource /dogs, you might write the following:

Code:
@GET
@Path("/dogs/{id}")
public String fetchDogName(@PathParam("id") int dogID) {
        return dogDAO.lookupNameById(dogID);
}

If you send a GET request to <site>/dogs/47598375833485934875, the web service will call fetchDogName(47598375833485934875) and serve it's return value.

...But let's say that's not very useful. Maybe you want to know more about the dog than just its name. Fortunately, RESTEasy (along with some useful providers like Jettison or Jackson) allows you to annotate objects for serialization into XML or JSON.

Now you could write a dog class which looks something like this:

Code:
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Dog {
    @XmlID
    @XmlAttribute
    private int id;
    @XmlAttribute
    private String name;
    @XmlElement
    private String breed;
    @XmlElement
    private String ownerName;
    @XmlElement
    private int age;
}

And with that in place, you can readily serialize a Dog object into JSON.

Now we can replace our old HTTP method with this one:

Code:
@Produces({"application/json"})
@GET
@Path("/dogs/{id}")
public Dog fetchDog(@PathParam("id") int dogID) {
        return dogDAO.lookupDogById(dogID);
}

....which will return a JSON representation of your Dog.

When you flesh out your code properly, you should be able to take in JSON representations of your object, and have it map them into a Java instance, use them in Java code, and then return them (or other objects) to be converted back into JSON. You could, for example, make a POST method on /dogs that would allow you to pass in JSON for a 'Dog' object, and it will create and store the new object on the backend and return an ID for it.

You'll likely want to look for documentation or other tutorials, though, of course. Let me know if you come up with any specific questions or problems.
 
Last edited:
Thanks Dogs and Hofan, so far this has been helpful. It looks like implementing the REST service will not be that difficult. I worry more now about the setup of the server and deployment. I still not sure which server I will be using. I could grab Tomcat.. They use Apache and are talking about moving to Nginx. I think they may be able to give me a separate server for this tool to live on but I'm not quite sure what's the best plan.

Do any of the JAX-RS implementations work on any webserver? Or if I need to use RESTeast with JBoss etc? I'm not really familiar with any of these... but it appears that JAX-RS specifies the annotations which is the same no matter which implementation I'm using.
 
Ok, I have a question. Does the server load and execute my application every time it receives a request? I'm pretty sure my application will need to load some setup data from a file in order to function. I'm not sure how long that takes and how quickly it would respond to requests. Dogs or someone, can you explain to me that works? Can my service "live" or be cached such that it doesn't need to do costly initialization each time a request is made?
 
Do any of the JAX-RS implementations work on any webserver? Or if I need to use RESTeast with JBoss etc? I'm not really familiar with any of these... but it appears that JAX-RS specifies the annotations which is the same no matter which implementation I'm using.

In theory you should be able to get any of the libraries to work on any platform, but if you're not experienced with using these libraries and the Java platform, this will probably be very painful.

The easiest way to do this would be to use middleware, because middleware will already have all of this set up for you. If you're on JBoss, JBoss already has stuff like RESTEasy and Jettison already configured. If you're doing the development in Eclipse, you just add the JBoss runtime to your build path, and you'll have access to all of these libraries. Then, when you want to deploy to a server, you can export to a WAR or an EAR and load it into JBoss. When you're doing the development, deploying to a local JBoss server for testing is fairly easy and can be done from within the IDE itself.

Does the server load and execute my application every time it receives a request?

No. Your application will be loaded once, in a phase called 'deployment'. Once it's deployed, it will stay running until it is either restarted or redeployed. From my understanding, RESTEasy uses a custom servlet class (mapped to say, /*) which receives the requests and then forwards them to your application (by calling the methods you wrote) based on the rules you defined in your annotations.
 
Thanks, do you have a suggestion on which implementation I should use? Is JBoss just an 'application server' or is it a web server or is there a difference?

So

I've got an Ubuntu VM setup to develop my application and they are running CentOS, I'm thinking that this difference shouldn't matter, but I'm just stating what I'm using.
 
Thanks, do you have a suggestion on which implementation I should use? Is JBoss just an 'application server' or is it a web server or is there a difference?

The only one I've worked with enough to actually stand up a web service is RESTeasy, which runs fairly easily on JBoss. Maybe the other ones are better...I wouldn't know, because I'm not a web services specialist. But RESTeasy will do what you need and will be fairly easy to get up and walking and talking.

JBoss is a middleware platform. It bundles a lot of service providers and libraries together to host your application. JBoss uses a servlet container called Apache Tomcat as what actually hosts the application as a server. Apache Tomcat contains a bunch of providers like Catalina and Coyote to connect your 'servlet container' to the HTTP requests that are coming in. It's one very large stack that builds upon different libraries and components available in the Java ecosystem.

Long story short: JBoss is an application server, but that also inherently means it's a web server...If you deploy a project full of only HTML files, it will be able to serve them. If you deploy a bunch of JSP pages and servlets that make up a dynamic website, it will be able to serve them. If you deploy a RESTful API, it will be able to serve that, too. If you deploy all of these things together, you'll have a 'web application' and this is why it's called an application server. Something like nginx or Apache, which are just 'web servers' won't be able to run an application. They can only serve HTML and run basic server side scripts....if you want to do more than that with nginx or Apache, you need to add more layers onto your stack, like PHP or Ruby on Rails, etc., which is why nginx or Apache wouldn't be considered an application server by themselves.

I've got an Ubuntu VM setup to develop my application and they are running CentOS, I'm thinking that this difference shouldn't matter, but I'm just stating what I'm using.

You should be able to install the JDK to your VM and put JBoss on it, and that should be all you need to host your application. If you need a persistence layer, like a database, that will be separate.
 
Yes rest is the way to go, I would suggest Jersey or Spring MVC's version. Jersey is more standard (An extension to JAX-RS), but spring is easier , at least to me, but for your case I'd probably go with Jersey since your not building an entire J2EE app :)

Jboss is not light weight, you should be ok with tomcat unless you want to screw with learning Jboss 7 OSGI for managing dependencies which is way overkill for a small project.

Actually here you go this should get you started.

http://www.mkyong.com/webservices/jax-rs/jersey-hello-world-example/

Jersey uses Jackson for JSON conversion behind the sceens you just simply tell the service what formats you want to allow in and out.

OP? Are you familiar with Maven at all?
 
Last edited:
unless you want to screw with learning Jboss 7 OSGI for managing dependencies which is way overkill for a small project.

He won't have to touch that even if he uses JBoss, since it's for an application of such a small scale.
 
He won't have to touch that even if he uses JBoss, since it's for an application of such a small scale.

Well If you want to use jboss 7 properly you would still need to create a module for Jersey. But really tomcat is much lighter weight and likely more appropriate for a single War file such as this would be. Unless they already have a jboss instance they are deploying other applications to that they want to re-use, or jboss is mandated for some other reason..
 
Well If you want to use jboss 7 properly you would still need to create a module for Jersey.

You mean if you wanted to use Jersey with JBoss 7 properly, you would need to do so. But, certainly if you have access to JBoss, you'd have no need to use Jersey since it already ships with RESTeasy.
 
You mean if you wanted to use Jersey with JBoss 7 properly, you would need to do so. But, certainly if you have access to JBoss, you'd have no need to use Jersey since it already ships with RESTeasy.

Honestly I haven't used RESTeasy, is that just a basic jax-rs implementation or is it an extension? Does it offer the ability to unit test your rest services right inside a standard JUnit test like Jersey does? I'm sure if jboss is bundling it either one of the two should be fine they all seem to be rather similar anyways. I found Spring MVC controllers to work just fine for me, since they are essentially rest services.
 
Last edited:
Honestly I haven't used RESTeasy, is that just a basic jax-rs implementation or is it an extension?

It's a JAX-RS implementation, yes. I'm not sure where the JAX-RS standard ends and extension begins, but I would imagine there's not a whole lot in RESTeasy beyond the RAX-RS standard.

Does it offer the ability to unit test your rest services right inside a standard JUnit test like Jersey does?

Yes...? Is there a JAX-RS implementation that doesn't?

I'm sure if jboss is bundling it either one of the two should be fine they all seem to be rather similar anyways.

Well they would be similar, since they all have to implement the JAX-RS standard. If they have to follow JAX-RS, there isn't a whole lot of room for variation. The biggest difference is things like what providers for marshalling and serialization they use.

I found Spring MVC controllers to work just fine for me, since they are essentially rest services.

...and if he wants to use those, there's plenty of documentation for Spring as well. Tutorials, too.
http://spring.io/guides/gs/rest-hateoas/
 
It's a JAX-RS implementation, yes. I'm not sure where the JAX-RS standard ends and extension begins, but I would imagine there's not a whole lot in RESTeasy beyond the RAX-RS standard.



Yes...? Is there a JAX-RS implementation that doesn't?



Well they would be similar, since they all have to implement the JAX-RS standard. If they have to follow JAX-RS, there isn't a whole lot of room for variation. The biggest difference is things like what providers for marshalling and serialization they use.



...and if he wants to use those, there's plenty of documentation for Spring as well. Tutorials, too.
http://spring.io/guides/gs/rest-hateoas/

Interesting:

http://monkieblankie.blogspot.com/2011/05/jax-rs-difference-between-resteasy-and.html
 
Actually here you go this should get you started.

http://www.mkyong.com/webservices/jax-rs/jersey-hello-world-example/

Jersey uses Jackson for JSON conversion behind the sceens you just simply tell the service what formats you want to allow in and out.

OP? Are you familiar with Maven at all?

No I am not familiar with Maven and coming from a .NET and Visual Studio background I have wonder what the hell these Java kids are thinking!

So to create a REST project:

1. mvn archetype:generate -DgroupId=com.mkyong.rest -DartifactId=RESTfulExample
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
2. mvn eclipse:eclipse -Dwtpversion=2.0

How is that useful not a hassle? In Visual Studio it would probably be something like

1. install project template
2. File -> new project -> rest service

btw, I liked that link but the author made no mention of how to deploy or build the project. He just magically shows it running in some sort of server? He skips very important information.
 
Anyway, I got a JBoss "Hello World" working with RESTeasy, but I agree it seems like overkill and while my purposes are academic I'm working with a company that some friends own which is commercial.

So I'm thinking about exploring Jersey and Apache Tomcat I guess as a possible lightweight and free version?
 
No I am not familiar with Maven and coming from a .NET and Visual Studio background I have wonder what the hell these Java kids are thinking!

So to create a REST project:

1. mvn archetype:generate -DgroupId=com.mkyong.rest -DartifactId=RESTfulExample
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
2. mvn eclipse:eclipse -Dwtpversion=2.0

How is that useful not a hassle?

If you're using Maven, you can do it that way....But you're not forced to. There are alternatives for dependency management on the Java platform, such as Ivy and Gradle.

It may not seem useful to you to do it that way for such a simple project, but when you're working with dozens of internal enterprise systems, using hundreds of external libraries, and need to be able to distribute the dependencies of a project to hundreds of developers, it only makes sense to use dependency management.

If you've ever done any real software development on the .NET platform, you've probably used something similar called NuGET.

btw, I liked that link but the author made no mention of how to deploy or build the project. He just magically shows it running in some sort of server? He skips very important information.

Set up your 'server' in Eclipse and point it to your Tomcat runtime. Then, Eclipse will offer you the option to Run/Debug on server.
Something like this should give you a rough idea of how to set this up.
http://jaimesantosalcon.blogspot.com/2012/02/adding-tomcat-application-server-to.html
 
Anyway, I got a JBoss "Hello World" working with RESTeasy, but I agree it seems like overkill and while my purposes are academic I'm working with a company that some friends own which is commercial.

So I'm thinking about exploring Jersey and Apache Tomcat I guess as a possible lightweight and free version?

Yes that would be fine.

Either will work for what you want to do.

Jboss is an enterprise application server while tomcat is just a web/servlet container. All you need for this is a servlet container for what you described. Actually I believe Jboss uses tomcat as it's servlet container anyways, so tomcat is a a part of jboss.

For example, If your creating an enterprise application and need to do load balancing, or have multiple applications with dependencies on each other you should probably go with Jboss over tomcat. Jboss has integrated JMS systems, hibernate support etc. Unfortunately when new versions of these technologies come out, overwriting the standard Jboss stack can be a nightmare (well for me anyways, I'm terrible at creating re-usable scripts to do that stuff).

Looks like Jeresy has become the reference implementation of JAX-RS where RESTeasy has added some additional functionality to it for working with EJBs etc. So I'm guessing that this is why we are using Jersey at work over RESTeasy (They can be sticklers for using reference implementations for things when applicable) even though we are using jboss for the container.

You don't have to use maven, but maven makes project setup easy. You just need to install maven and add it to your path, then following the directions in that link and it will create the project for you from an archatype (basically gives you pre-setup Application with dependencies included). Basically this downloads the project and it's dependencies form an online repository. You can still use eclipse or Intellij etc to build your project that you created with maven as most IDE's have maven integration (Use import maven project option). The catch 22 is whether they are using maven 2 or 3. I think 2 is still more common but it's often a detail left out in many postings likely due to maven 2 being the standard for some time and being I believe backwards compatible with 1). I don't think 3 is completely backwards compatible with 2 but it can be migrated if you know the gotchas (I'm not a maven expert). You can change or edit project dependencies by editing the pom.xml which is the core file for maven.

Maven is a whole topic in itself so I won't get into that :)
 
Last edited:
Thanks guys, got Apache Tomcat running and looking into Jersey and Maven. Give me a bit to play around with it. I would have replied earlier today but [H] forum was down with login issues. I really appreciate the help.

As far as Nuget.. I've used it some. I've been writing software for some time now (7+ years), but we don't have a lot of external dependencies.
 
I really hate Maven it doesn't seem to work at all.

I installed eclipse, maven, etc. I wanted to created a Jersey 2.6 application so I tried the following from https://jersey.java.net/download.html

mvn archetype:generate -DarchetypeGroupId=org.glassfish.jersey.archetypes -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeVersion=2.6

So Maven asks me for 'groupID' 'artificatId' etc. etc.. like I'm supposed to have more information. It's supossed to do this all for me, no?

So I downloaded all the Jersey JAR files, but I have no idea which JARs I need for my application if I create it manually. Other examples use Jersey 1.X and the JARs have different names or something? All I wanted to do was create a simple REST service with Jersey.

P.S.

I have no idea what a POM file is or does other than specifying dependencies in some manner. Parent POMs and all that stuff doesn't mean anything to me right now. Also, on Jersey's page wtf is a "Grizzly container" and what is the proper archetype for just a plain old REST service that I plan to install to Tomcat 8 ?
 
Also the tutorial (myong) you gave me doesn't work either. It generates a mostly blank directory structure with nothing but a mostly blank POM file and web.xml and nothing else.
 
ok I got the above video tutorial working by switching to Web Project 2.5 instead of 3.0 and downgrading to Jersey 1.X instead of 2.X. Oh yeah, and that also gives me the proper web.xml editor.

I guess everything is completely different with the newer versions.. To be honest, I don't really need Maven. I just need to get this working for me and myself only and I would like to understand how and where the dependencies come from and where they are put before some tool does it for me.
 
Back
Top