reset password
Author Message
abhishek_sharma
Posts: 79
Posted 17:54 Aug 20, 2012 |

Starting from Spring 3.0 , it provide support for Restful services.

I am trying to learn and implement this feature, but having little difficulty in order to configure it .

Considering an example :

Having an User controller with a method to

@RequestMapping(value="userDetails.html", method = RequestMethod.POST)

public String userDeails(@RequestParam(value = "userId",require = true) Long userId{

//This should return data as a JSON Object

}

So on this

$(ajax{

url : "/userDetails.htm;"

// I should  get JSON data

});

So instead of changing anything in Controller I want to implement a Service (/api/userDetails/1) which will further call this controller and then this Sevice can return data in any required format.

It getting little confusing in eclipse to create files as Spring3.0 and above project in Eclispse creates JAX-WS Webservice with two subfolder.

If anyone can tell me which file should go where ??

 

cysun
Posts: 2935
Posted 18:11 Aug 20, 2012 |

In this case you don't need jax-ws. Just change your controller a little bit so a) it uses @PathVariable to get the user id, e.g. @RequestMapping("/api/userDetails/{id}"), and b) it returns a JSON response, e.g.

public String controllerMethod( ...., HttpServletResponse response )

{

    ...

    response.setContentType("application/json");

    response.getWriter().print( jsonObject );

    return null;

}

abhishek_sharma
Posts: 79
Posted 18:14 Aug 20, 2012 |
Thanks you for such an early response
 
cysun wrote:

In this case you don't need jax-ws. Just change your controller a little bit so a) it uses @PathVariable to get the user id, e.g. @RequestMapping("/api/userDetails/{id}"), and b) it returns a JSON response, e.g.

public String controllerMethod( ...., HttpServletResponse response )

{

    ...

    response.setContentType("application/json");

    response.getWriter().print( jsonObject );

    return null;

}