0

I have the following code that works:

@RequestMapping(value = "/jsonasclass", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody
ContactVO jsonAsClassPost(@RequestBody ContactVO ct){
    ct.setFirstName("This-property-is-changed-in-the-controller");
    return ct;
}

and the corresponding ajax call by post:

$.ajax({
            url: '/jsonasclass/',
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify({
                  id:1,
                  userID:1.1, 
                  login:'sample-login',
                  firstName:'sample-first-name'
                }),
            contentType: 'application/json',
            mimeType: 'application/json',
            success: _callBack,
            error: _errorCallback
        });

Now I want to achieve the same thing, but I want to do it by GET. Anyone knows how?

  • I have tried changing POST to GET (in both controller and ajax call) but it did not work.
  • The error I get: description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
faisal
  • 1,327
  • 10
  • 19
  • Try changing to 'GET' in ajax call and in @RequestMapping 'method' attribute as well – Keerthivasan Dec 13 '13 at 14:58
  • Yes, I changed in both places. But it did not work. – faisal Dec 13 '13 at 15:01
  • Use Firebug to track the GET request – Keerthivasan Dec 13 '13 at 15:02
  • yes, I tried all of the standard methods. I get this error: description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method. – faisal Dec 13 '13 at 15:05
  • Either it is clearly a build/compilation problem or you have another method mapped with the same url with different HttpMethod type. Try clean and create a new build – Keerthivasan Dec 13 '13 at 15:07
  • 1
    There is no build/compilation problem. The `HTTP GET` is not meant to work that way (passing content on the request). See [this](http://stackoverflow.com/questions/13270708/spring-resttemplate-http-get-with-request-body) and [this](http://stackoverflow.com/questions/978061/http-get-with-request-body). The common way to pass parameters to a `GET` is via URL parameters. – Raul Rene Dec 13 '13 at 15:15
  • @RaulRene `data` is actually [appended to the URI for GET requests](http://api.jquery.com/jQuery.ajax/) – Floegipoky Dec 13 '13 at 15:21
  • When you changed the method to GET did you also change the `@RequestBody` parameter? – Floegipoky Dec 13 '13 at 15:23
  • 1
    @Floegipoky Yes, but you append **strings**, not objects to the URL. He should pass `id`, `userId`, etc as strings and retrieve them using the `@PathVariable` or `@RequestParam` annotations – Raul Rene Dec 13 '13 at 15:23
  • He is using `contentType: 'application/json'`, which is telling the server: *hey, I'm sending you a JSON* – Raul Rene Dec 13 '13 at 15:26
  • RaulRene's comment actually helped. I figured out how to do it from his comment. Thanks :) I will post the answer soon. – faisal Dec 13 '13 at 15:26
  • @RaulRene I'll be the first to admit that my js knowledge isn't exactly top-notch, but isn't that what `JSON.stringify` is for, converting the json to a string? As I implied in my last comment though, I agree that the `@RequestBody` probably won't work for GET – Floegipoky Dec 13 '13 at 15:28

1 Answers1

1

Thanks to RaulRene's comment here is how you would do it.

  • get rid of @RequestBody from the controller and change method to get.
  • send the properties of the class in the controller as browser variables and spring will automatically map them to the class.

Here is the solution:

@RequestMapping(value = "/jsonasclass", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody
ContactVO jsonAsClassGet(ContactVO  ct){
    ct.setFirstName("This-property-is-changed-in-the-controller");
    return ct;
}

and corresponding ajax:

    $.ajax({
        url:'/jsonasclass/',
        type: 'GET',
        data: {
              id:1,
              userID:1.1, 
              login:'sample-login',
              firstName:'sample-first-name'
            },
        success: _callBack,
        error: _errorCallback
    });
faisal
  • 1,327
  • 10
  • 19
  • BTW, make sure that the class you are accepting as a parameter and the class you are returning implements Serializable. – faisal Dec 17 '13 at 16:59