Search code examples
javaapigrails

How to not associate a controller with a view? [Grails]


I am working on a Grails app that only consumes a third-party API and uses Firebase as a datastore. Because of this, I don't want to associate my controller methods with any views in the grails-app/views directory. I keep getting a ServletException: could not resolve view in servlet whenever any controller methods gets called back. How should I stop this from happening?


Solution

  • In Groovy if the last statement of the method is implicitly the return statement. Similarly, in Grails, a controller action method will expect a view with the same name as your method name if you do not explicitly render a view.

    With that in mind, you can solve this in two ways.

    1. Create an empty view that matches the method name with nothing in it.

    2. Render an empty string as the last statement in your method as such.

      class SomeController{
      
          def index(){
             // do stuff
             render ""
         }
      }