tags:

views:

33

answers:

2

Hello!

I started a new job in a company that loves using the word, "enterprise." As such, everything they've written is in Java. I come from a very web-development heavy background, mainly working with LAMP stacks.

Now, up until three days ago I knew nothing about Java other than people used it and that it is a programming language. Googling up on it, the Java language itself seems simple enough. However, when people say, "Java" it seems they are referring to more than just the language, such as the various frameworks and application servers. It's a little over-the-top, and am having some trouble getting up to speed with "Java."

An upcoming project involves me creating an exposed REST API for one of the products. Seems easy enough. However, I have some questions about how to proceed....

I'm working with JBoss AS for the first time; not sure if there's an equivalent in PHP so I can understand what JBoss does exactly, but I suspect there's a "proper" way of doing things. Here's what I was thinking of doing:

1) Created a package with a single servlet, like so:

package com.awesome.myrestapi;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HiggiltyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public HiggiltyServlet() {
        super();
    }

    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();

        // @todo ideally, do something more RESTfully useful and less vindictive
        out.println( "<html><body>HAHA!  all ur api requests are belong to us</body></html>" );
        out.close();
    }

}

2) As you can see, I was thinking of just overriding the service method to serve my REST API requests.

3) Updated my web.xml file accordingly, so that the url-pattern would match "higgilty", thus making my URL endpoint something like....

http://localhost/awesomeproject/higgilty

Now, I feel like I might be doing something wrong. Am I going about this the right way, or am I totally off the mark?

Any help is greatly appreciated.

+2  A: 

You should probably use a REST-oriented framework like Restlet, Jersey or RESTeasy. This will help you deal with various things like splitting URIs, having a system based on resources and representations and perhaps content-type negotiation (if you need it).

Restlets can run within a servlet container or as standalone applications. RESTeasy is a JBoss project, but I wouldn't dismiss the other frameworks just because of that, since JBoss AS should in principle be able to run applications written with the other frameworks (I've run Restlet applications within JBoss AS successfully,, although I don't use it regularly).

Bruno
Ah, Thanks for the explanation. I'll give it a shot. Thank you very much!
sayajay
+1  A: 

Agreed it's probably best to use a framework.

If you want to roll your own, you need to write something to parse the URL and route to the appropriate method. The URL pattern matching you get with web.xml is pretty limited.

Also you'd probably want to override the HttpServlet methods that correspond to HTTP methods - doGet, doPost, etc.

billintx
Thanks for taking the time to comment!
sayajay