How to switch a Spring Roo or Grails project from regular MVC to AJAX / JSON / REST

This may seem like a strange question, but I'm trying to figure out what β€œbest practice” is for converting an application that is configured to use something like generating Roo or Grails controllers (which provides basic CRUDs) to what returns the JSON response body instead for use in a JavaScript application.

The ambiguity of the technology here is that I have not yet started the project. I'm still trying to decide which technology (based on Java) to use and see what productivity tools I should learn / use in the process. It will be a web application and it will use the database persistence tier. All other parts are in the air.

Perhaps the easiest way to achieve my goal is to start using some kind of AJAX plugin to get started, but most of the manuals and descriptions out there say to start with a normal MVC architecture. Roo seems to do the conversion of the controllers that it generates for JSON-friendly return types is difficult, and I'm not familiar with Groovy / Grails enough to know what it takes to do this.

This is mainly a learning experience for me, and I am open to any criticism or advice, but as a Q / A forum, I understand that I need to include an objective question. To satisfy this need, I ask:

What is the best way to customize the AJAX / RESTful interface for my objects in Roo and / or Grails?

+7
source share
1 answer

I recently did this with the Grails application and found it is surprisingly easy to take the generated controllers and make them output JSON or XML or HTML from the view depending on content consistency.

The places to learn in the Grails tutorial are the content content section and, if you need to deal with JSON or XML input, marshaling.


To get the JSON and XML output in the list() method by default, he changed it to this (I have a Session object, in this case ... one of my domain classes):

 def list() { params.max = Math.min(params.max ? params.int('max') : 10, 100) def response = [sessionInstanceList: Session.list(params), sessionInstanceTotal: Session.count()] withFormat { html response json {render response as JSON} xml {render response as XML} } } 

withFormat you return the default object, you need to replace the return value with the withFormat block.

You may also need to update the Config.groovy file, where it deals with the mime type. Here is what I have:

 grails.mime.file.extensions = true // enables the parsing of file extensions from URLs into the request format grails.mime.use.accept.header = true grails.mime.types = [ html: ['text/html','application/xhtml+xml'], xml: ['text/xml', 'application/xml'], text: 'text/plain', js: 'text/javascript', rss: 'application/rss+xml', atom: 'application/atom+xml', css: 'text/css', csv: 'text/csv', all: '*/*', json: ['application/json','text/json'], form: 'application/x-www-form-urlencoded', multipartForm: 'multipart/form-data' ] 

As input (for example, for the update() or save() action), the JSON and XML data will be automatically unmarshaled and will be bound in the same way as the form input, but I found that the markup process is a bit picky (especially with JSON) .

I found that for the correct JSON handling in the update() method, the class attribute must be present and corrected on the incoming JSON object. Since the library that I used in my client application did not help solve this problem, I switched to using XML instead.

+8
source

All Articles