Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to serve both GSPs and JSON from a Grails 3.1.5 app

I wanted my Grails 3.1.5 app to serve both JSON data using the *.gson format AND, for some pages/URLs I wanted to continue to use GSPs.

I built an app using the rest-api profile. Then I copied over controllers and views from an other app that I'd built using the web-api.

In doing so, and to be consistent, I also moved index.gson to a different location.

Now I get a:

Could not resolve view with name 'index' in servlet with name 'grailsDispatcherServlet'

Started digging into the viewResolvers that are available in the 3.1.5 code base. It is possible that the rest-api profile configures a viewResolver to look for *.gson files in a certain location.

Is there anyway to configure maybe a CompositeViewResolver that looks for both the views, *.gson and *.gsps?

If so, how can I do this?

Thanks!

like image 362
Pankaj Tandon Avatar asked Apr 24 '16 15:04

Pankaj Tandon


1 Answers

I've managed to resolve this issue by adding this plugin to build.gradle:

compile 'org.grails:grails-plugin-gsp'

and with both

profile 'org.grails.profiles:web'
profile 'org.grails.profiles:rest-api'

and apply plugins

apply plugin: 'org.grails.grails-web'
apply plugin: 'org.grails.grails-gsp'
apply plugin: 'org.grails.plugins.views-json'

Apparently, they remove it when you're using REST profile, to reduce overhead, as you rarely render HTML on REST service side.

like image 67
Jezdimir Lončar Avatar answered Oct 24 '22 06:10

Jezdimir Lončar