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
                }
            });

        };
neverwinter
  • 810
  • 2
  • 15
  • 42
  • 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: http://stackoverflow.com/questions/21245712/unable-to-send-json-object-to-rest-web-service – rob Jul 02 '14 at 15:00
  • I need to find solution in spring :) – neverwinter Jul 02 '14 at 15:02

1 Answers1

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.

QBM5
  • 2,778
  • 2
  • 17
  • 24
  • This post explains how you can read the data server-side: http://stackoverflow.com/questions/1548782/retrieving-json-object-literal-from-httpservletrequest – rob Jul 02 '14 at 15:12