0

I am trying to create REST web service with grails 5.1.7. Have checked the documentation for Grails REST on its official site and followed the steps to create services.

Currently got a situation where a webflow kind of feature needs to be performed, so to deal with we will be creating a single action which will handle session objects (which are to be transferred between steps of the webflow). Now the matter is how the GSON view will be resolved for each steps, as there will be single action will be serving all the steps of the webflow (there are 6 steps of the webflow). With HTML/GSP response of grails we can pass on "view" file to be used for any action. But for REST response, how to specify which GSON view should respond the result of the step?

I tried respond (just to experiment) (view: "handleDefineStep", model: modelMap), but it did not work. The document says that view and model should be used for HTML response so it is clear that it would not work.

One thing I would like to note that there is no domain classes in our project so domain rendering will not be there. Data are transferred using command objects created under src/main/groovy.

A sample code snippet for webflow is like:

def initializeFlow() {
   // ... initialize some objects and store them to session to be used at other actions
   def currentStep = params.step
   
   switch(currentStep) {
      case "step1":
         handleStep1() // This action has its own GSON response to respond with
         break;
      case "step2":
         handleStep2() // This action also has its own GSON response to respond with and likewise
         break;
      // ...
   }
   // ...
}

Update handleStep1 method:

def handleStep1() {
   // ...
   respond (model, view: "editPage/handleDefineStep")
}
Parth Bhagat
  • 509
  • 1
  • 11
  • 24
  • `render(view: "handleDefineStep", model: modelMap)` is valid, and assuming all the content negotiation stuff is configured correctly, will render `grails-app/views/[your controller name here]/handleDefineStep.gson`, and the contents of `modelMap` will be available in the view. Is that not what you are seeing? – Jeff Scott Brown Feb 09 '23 at 20:08
  • Yes. Here `handleStep1()` method would handles the request now onwards if the step is `step1`. I have handleStep1.gson file defined with simple content, but it does not seems to be called, it returns default response e.g. `render(view: "handleDefineStep", model: modelMap)` returns response as `{"view":"handleDefineStep","model":null}`. `handleDefineStep.gson` file is located under directory `grails-app/view/controller/`. Also, the grails project is created using `web profile` and not with `rest-api profile`. – Parth Bhagat Feb 10 '23 at 09:56
  • I don't understand that unless the app is invoking something after `render` that isn't mentioned in the code. I am sorry that I can't help. If it looks like a bug, please file a report at https://github.com/grails/grails-core/issues with a sample app and we will investigate. Best of luck! – Jeff Scott Brown Feb 10 '23 at 13:13
  • No no, it is not invoking any code after `render` called. Thank you for the help on looking at the matter. Found the fix for it. Actually it is there on the `respond`'s document (https://docs.grails.org/latest/ref/Controllers/respond.html) only. There is a note at the bottom of the page with highlighted box, the order of the parameter needs to be taken care. Made an update in the original post. Calling signature of respond has changed to `render(mode, view: 'editPage/handleDefineStep')`. And this works. Thank you for the hints I got from your input. – Parth Bhagat Feb 10 '23 at 14:24
  • Your last comment is talking about `respond`. I thought you were using `render`. So sorry for the confusion. I see now the question has been edited to include `respond`. – Jeff Scott Brown Feb 10 '23 at 14:29
  • Yes, initially I was using `render` only. But it did not work out somehow and then found `respond` on some forum and then on official documentation of Grails. Thank you for the hint on this matter. It helped me to find the fix for my issue. – Parth Bhagat Feb 10 '23 at 15:15

0 Answers0