开发者

JSON Client side API for spring MVC 3.0

I am looking for a way to setting up a JSON proxy client in a spring framework way.

We are going to use Spring MVC on the server side. We don't like XML as they are overkill and heavy. JSON seem to be a lightweight and effective message container for us.

However, I've search around and read http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html many times and I don't find any hits to put my spring client into a开发者_高级运维 JSON client.

They provided RMI, Http, JAX-WS, SOAP and others. But nothing related to as a client of MVC (which I guess it could be common as we don't want to write it twice)

RestTemplate looks good but I am wonder is it the suggested way to do in spring 3.0.


The RestTemplate is indeed the preferred way of accessing rest services.


I've been in the same position as you - looking through the Spring docs for how to implement a simple JSON client API. I ended up implementing it myself, as I only needed it for a few RPC-like calls to another webapp. IIRC Jax-RS has this capability so you might want to invest in implementing it - for my needs it seemed overkill.

All there is to it:

  • write a simple method to perform the HTTP GET to the JSON web service and return a String (I used Apache HttpClient)
  • pass the String to Jackson to deserialize into a Java object (see mapper.readValue())

This assumes you already know what kind of object you expect to get back from a given JSON web service.

As an aside, the other thing I needed from my Spring MVC JSON web service was the ability to do JSONP (cross site callback) for consumption in the browser with JQuery (note: JSONP is not secure so use at your own risk). The automagic Spring JSON webservices that Bozho outlined does not provide an option for JSONP. The easiest way to provide JSONP is to implement a simple servet filter.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜