I am working on transforming a Grails 2.4.5 application from a website to a web service by transforming most of the service classes to REST Services that will serve a UI.
I encountered an issue when trying to create a simple action to return a list of objects of a simple class, which is a simple DTO class used to only display multiple serialized data coming from multiple domains.
The action inside the controller works fine and returns some JSON data from the Browser as well as from Postman.
However, I can't use Curl with a Bearer Access token to get the data, as it redirects to the home page. I am not sure why there's that redirect. When I look at the logs, the request with Curl doesn't even enter the controller. I enabled some log.info() message at the first line of the action/method. I can see that message when the app is launched from the browser and Postman, but not when using CUrl, since the request never gets processed by the action inside the controller.
I am really confused as I am not sure what can be the cause. Any help to point me in a right direction will be helpful.
@Secured(['IS_AUTHENTICATED_FULLY'])
class ClassController extends RestfulController{
static responseFormats = ['json']
static allowedMethods = [actionDTOs : "GET"]
static defaultAction = "actionDTOs"
static scope = "singleton"
def actionDTOs() {
log.info("inside controller /listdtos");
def listDTOs = getAllDTOs() //private action/method inside controller
log.info("listDTOs: $listDTOs")
respond listDTOs, [status: 200]
}
private def getAllDTOs() {
..
}
}
class UrlMappings {
static mappings = {
"/api/v1/listdtos/$size?"(controller:'class', action:'actionDTOs', method: 'GET')
"/$controller/$action?/$id?" {
constraints {
// apply constraints here
}
}
/**
** This is where the action "**actionDTOs**" is redirected when I try to use CUrl
** as well as when the front-end tries to access it.
** Why is that I can access the data properly from Browser and Postman?
**/
**"/"(controller: 'welcome', action: 'home')**
}
}
class listDTOs implements Serializable{
...
}
To create a Restful Controller, we usually extend the RestfulController or use @Resource on top of our domain> . Since not of that applies in my case, what's the workaround to return my non-domain class through a GET request so Grails knows it's a REST Controller not linked to a domain?
It depends on what you mean by "Restful Controller".
You could have a controller like this:
I know that
listDtois not a domain class so I can't show code of how to retrieve them or update them but presumably you know how to do that.EDIT:
You don't. You could use this:
Then of course in your URL mapping you map whatever urls you like to those actions, as in the previous example.