burns
burns

Reputation: 1121

How do you render the grails 404 GSON template with the rest-api profile with the gsp dependency added

I have built a grails 4.0.10 app with the rest-profile.

Then added the console plugin. (because it is just too handy) compile 'org.grails.plugins:grails-console:2.1.1'

Which has a dependency on gsps. compile "org.grails.plugins:gsp" // console plugin needs to render gsps.

Once the gsp plugin is added, the 404 handler now renders a default notFound.gsp instead of the notFound.gson file.

Is there a way to set up URL mappings to render the .gson view by default instead of the .gsp one?

The only mechanism I found was to create an error controller to handle it manually, but I feel like i'm overlooking something really simple in UrlMappings.

class UrlMappings {

    static mappings = {
        ...
        "404"(controller: "error", action:'notFound')
    }
}

and then creating an errorController to render the view.

class ErrorController {
    static responseFormats = ['json', 'xml']
    
    def notFound() {
        render(view: "/notFound")
    }
}

Here is hoping for "404"(view: '/notFound', pleaseUseGson: true) setting that I haven't found.

Upvotes: 0

Views: 160

Answers (1)

burns
burns

Reputation: 1121

The system is getting confused because there is a notFound.gsp entry hidden in the cache plugin.

the views.properties file has this line

/WEB-INF/grails-app/views/notFound.gsp=gsp_cachenotFound_gsp

I'm not entirely sure why this view from the cache plugin is being used before the one in my app, but that's a problem for later.

By renaming the notfound.gson file to something else seems to circumvent the problem.

so change

"404"(view: '/notFound')

to

"404"(view: '/notFound404')

and rename the file notFound.gson to notFound404.gson.

Upvotes: 1

Related Questions