开发者

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


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜