Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation on how to use the plugin with the withFormat block #1

Closed
cdeszaq opened this issue May 2, 2012 · 1 comment
Closed

Comments

@cdeszaq
Copy link

cdeszaq commented May 2, 2012

This plugin is great, but I'm not quite sure how to use it with the withFormat grails block.

In particular, I'm looking at this from the standpoint of versioning a REST api via mime-types, similar to how Github does it.

Here is a rough idea of the sort of use pattern / setup I envision:

Config.groovy

// ... snip ...
grails.mime.types = [html: ['text/html', 'application/xhtml+xml'],
        text: 'text/plain',
        js: 'text/javascript',
        rss: 'application/rss+xml',
        atom: 'application/atom+xml',
        css: 'text/css',
        csv: 'text/csv',
        all: '*/*',
        form: 'application/x-www-form-urlencoded',
        multipartForm: 'multipart/form-data',

        jsonv3:['application/vnd.myapp','application/vnd.myapp+json','application/vnd.myapp.v3+json'], // Default, default json, and v3 json
        jsonv2:['application/vnd.myapp.v2+json'], // Only v2 json
        xmlv3:['application/vnd.myapp+xml','application/vnd.myapp.v3+xml'], // Default xml, and v3 xml
        xmlv2:['application/vnd.myapp.v2+xml'] // only v2 xml
]

// Having to declare this once for XML and once for JSON feels clunky
grails.plugins.marshallers.xml = {
    v2{
    }
} 

grails.plugins.marshallers.json= {
    v2{
    }
} 
// ... snip ...

FooController.groovy

// ... snip ...
    def show() {
        def object = Foo.get(params.id)
        withFormat {
            html {object}
            jsonv3 { render fooInstance as JSON} // v3 is the default
            jsonv2 {
                JSON.use('v2') {render fooInstance as JSON}
            }
            xmlv3{ render fooInstance as XML} // v3 is the default
            xmlv2{
                JSON.use('v2') {render fooInstance as XML}
            }
        }
    }
// ... snip ...

FooV2XmlMarshaller

import org.codehaus.groovy.grails.web.converters.marshaller.ObjectMarshaller
class FooV2XmlMarshaller implements ObjectMarshaller<XML> {

    static configuration = "v2"

    boolean supports(Object object) {
        Foo.isAssignableFrom(object.class) 
    }

    void marshalObject(obj, xml) {
        // Special v2 stuff
    }
} 

Now, it might be that the plugin already enables this, which would be fantastic, but when I tried it myself, it didn't seem to want to work.

@pedjak
Copy link
Owner

pedjak commented May 11, 2012

Sorry for the late response.

As far as I know - the things should work as you described.

Could you provide me more details what does not work? In particular:

  • do you land in marshallObject method of FooV2XmlMarshaller ?
  • do you land in jsonv2 and xmlv2 closures of FooController ?

@pedjak pedjak closed this as completed Aug 19, 2013
aruizca added a commit that referenced this issue May 8, 2014
Re-initialize marshallers when a domain or config changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants