29

Is there a way to build Spring Web calls that consume and produce application/json formatted requests and responses respectively?

Maybe this isn't Spring MVC, I'm not sure. I'm looking for Spring libraries that behave in a similar fashion to Jersey/JSON. The best case would be if there was an annotation that I could add to the Controller classes that would turn them into JSON service calls.

A tutorial showing how to build Spring Web Services with JSON would be great.


EDIT: I'm looking for an annotation based approach (similar to Jersey).

EDIT2: Like Jersey, I am looking for REST support (POST,GET,DELETE,PUT).

EDIT3: Most preferably, this will be the pom.xml entries and some information on using the spring-js with jackson Spring native version of things.

2
  • Similar topic is raised here: stackoverflow.com/questions/658936/… Commented Mar 25, 2010 at 11:19
  • dma_k - that question really isn't that similar. That question asks about converting between java objects and JSON , this one asks specifically how to use Spring MVC to send and receive JSON messages. Commented Mar 25, 2010 at 15:12

8 Answers 8

32

In case other ppl get here later: http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/ was the most useful link for me. That finally made me understand the model (after less than a day of searching).

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

1 Comment

This is much better if you're already at Spring 3. The Spring-JSON project is great, but it unfortunately relies on Spring 2.5.
17
+50

You can certainly have controllers that return a View which is in JSON output. Take a look at the Spring-JSON project.

To consume requests in JSON I would assume you would just want a controller to pass a request parameter off to a JSON library which could parse the data?

1 Comment

This isn't exactly what I want. It should work like Jersey, I don't want to hand things off, the MVC controller should be wrapped with a JSON aspect layer (probably through annotations) that allows it to send and receive JSON. The Spring-JSON project does half of that (sending) but not the other half.
4

There is no pre-packaged way to do what you want as Jersey is nicely integrated with Spring via the Jersey-Spring API so there's really no reason to re-invent the wheel.

1 Comment

I currently use Jersey and it's good. To be complete honest, I don't find the Spring integration all that compelling, which isn't really a big deal. Much like Spring MVC versus Struts, I'm not really expecting a lot of change with the Spring Rest JSON functionality, I'm just trying to figure out if it actually exists so I can try it out.
3

Check this one out

Adding support for JSON and XML views

Source code for Spring Finance Manager

2 Comments

These tutorials had references to source code - do you know where the links are to said source code?
Added the link to the sources
2

Since spring-mvc 3.0 official support for Ajax remoting with JSON is provided as part of Spring MVC. This includes support for generating JSON responses and binding JSON requests using the Spring MVC @Controller programming model.

see here

Comments

2

This feature is now part of Spring since version 3.0. You can simply use the @ResponseBody annotation to specify that you want the return value from your request handler methods to be serialized to JSON and sent as the response body. See http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/

Comments

0

It seems that DWR framework looks very close to what you want to get.

Comments

0

You can configure Spring MVC interceptors through handler mappings and then use the mappings to transform the returning data to JSON. The handler configuration can be done in xml (no need to recompile), and this can be completely transparent to the rest of the application.

It's fairly easy to annotate controllers with some annotation, and then hook up the BeanFactory bootstrap process to register the controllers within some handler mapping process.

I have used this approach to transform the result from spring controllers into GWT RPC calls.

You can also use Spring AOP to intercept controller method calls and unwrap/wrap the requests from/to JSON.

Comments

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.