1

I have a restful web service which written in java. And I call it from javascript. I pass a string parameter on it (param=5). But I need to send an object to pass it. How can I pass an object in restful webservice from javascript side? And how can I parse it in java side?

java code :

@RequestMapping(value = "/services/getVillages")    
@ResponseBody

    public  Village getAllVillages(HttpServletRequest request) throws JSONException {

             String param = request.getParameter("param");
             System.out.println("parametre: " + param  );   
                   long startTime = System.currentTimeMillis();
                   //Village result = innerService.getAllVillagesCacheable(request);
              long stopTime = System.currentTimeMillis();
              long elapsedTime = stopTime - startTime;
              System.out.println("süre: " + elapsedTime);         

              startTime = System.currentTimeMillis();
              //Village result2 =   innerService.getAllVillages(new HttpServletRequest());
              stopTime = System.currentTimeMillis();
              elapsedTime = stopTime - startTime;
              System.out.println("süre cache'siz: " + elapsedTime);       

              return new Village();     }

javascript code :

function callWS()
            {

            $.ajax({
                type: 'GET',
                url: 'http://localhost/services/getVillages/',
                data: "param=5", // the data in form-encoded format, ie as it would appear on a querystring
                dataType: "json", // the data type we want back, so text.  The data will come wrapped in xml
                success: function (data) {
                    alert("party hard"); // show the string that was returned, this will be the data inside the xml wrapper
                },
                error: function (data) {
                    alert("restful cagirmada hata"); // show the string that was returned, this will be the data inside the xml wrapper
                }
            });

        };
2
  • I would recommend using JAX-RS instead of Spring RequestMapping if your architecture allows.. You can then use the @Consumes annotation. Have a look at this post: stackoverflow.com/questions/21245712/… Commented Jul 2, 2014 at 15:00
  • I need to find solution in spring :) Commented Jul 2, 2014 at 15:02

1 Answer 1

1

While you can pass objects with a get event by putting the object into the data property of the request I would suggest using a post method. The post method allows your object to be put into the body of the request which is much easier to pass when dealing with complex objects.

$.ajax({
            type: 'POST',
            url: 'http://localhost/services/getVillages/',
            data: {val1: 1, val2: 2, val3: 3}, 
            dataType: "json",
            success: function (data) {
                alert("party hard"); 
            },
            error: function (data) {
                alert("restful cagirmada hata"); 
            }
        });

doing it as shown above will create a post request and add the object into the body of that request.

With simple objects you could do this with a get, and it would put the properties into the querystring, but I suggest using a post and putting the object in the body.

Sign up to request clarification or add additional context in comments.

1 Comment

This post explains how you can read the data server-side: stackoverflow.com/questions/1548782/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.