wmitchell
wmitchell

Reputation: 5735

REST request to JAVA Servlet

I have some JavaScript which I want to perform a REST Request (GET) to my servlet. The format of the record I want to send is in the following format ...

/id1/vara/varb/varc/timedelta1,timedelta2,timedelta3,....,timedeltaN/ 

So basically there would be 5 attributes in each record I send. I need to batch these up - I'm sending multiple records in a single GET Request. My Get URL might look a little like the following.

myservletname/id1/vara/varb/varc/timedelta1,timedelta2,timedelta3/id2/vara/varb/varc/timedelta1,timedelta2,timedelta3/id3/vara/varb/varc/timedelta1,timedelta2,timedelta3/  

I'm aware on the limit of around 2000 chars in the URL String so to keep things safe I'll ensure the length of the URL is less than this. In the above example 3 records were sent to the servlet.

I'm wondering how I might process these on the server end. Havent really worked with REST before in Java. What do I need to do on the server end to process these URLs to extract the data ?

Thanks

Upvotes: 1

Views: 804

Answers (3)

ptomli
ptomli

Reputation: 11818

Basically

public class RestServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        String uri = request.getPathInfo();
        Pattern p = Pattern.compile(
            "/([^/]+)/([^/]+)/([^/]+)/([^/]+)/(\d+)(?:,(\d+))*/"
        );
        Matcher m = p.matcher(uri);
        if (m.matches()) {
            String id = m.group(1);
            String vara = m.group(2);
            String varb = m.group(3);
            String deltas = m.group(4);

            // etc
        }
    }
}

It's not a very good model for how to do it, but it is simple and understandable for someone not familiar with Servlets

Upvotes: 1

Met
Met

Reputation: 3172

You can use JAX-RS or Restlets instead of a servlet

Upvotes: 1

Qwerky
Qwerky

Reputation: 18455

You should seriously consider using POST instead of GET for this. REST (and URLs) weren't designed for this purpose.

Upvotes: 0

Related Questions