Grails 2.4.5 Rest API GET Request: Can't get Response Body from CUrl and from React UI, but can get it from Postman and Browser

Viewed 61

Grails 2.4.5 REST Controller returning List of non Domain object DTO

What's the best way to turn a method/action into a REST Resource accessible through a GET request, without returning a domain class/entity, but with a non-domain/entity DTO class?

With SpringBoot, it will look like this:

@RestController
class EmployeeController { ...}

/**
**I am not sure that even in Spring/SpringBoot you can return a list of non entity class DTO.
** Can you return a random non entity 
**/
@GetMapping("/employees")
  List<EmployeeDTO> all() { 
    return repository.findAll();
  }

For my Grails project, I am trying to return a list of DTO object having data from different domain/entities. Since the class is not a Grails domain class, how do you do that, since it doesn't follow the Grails convention?

I have the code as below:

class ClassNameController extends RestfulController{

/**
** Grails usually wants you to use call the parent constructor and and put a domain class as 
** a parameter like super (DomainClass). 
** In my case, since I have a non-conventional Controller which combines different data
** sources/domains/entities, how do I implement my method/action to turn it into a Grails REST
** Resource?
** Is there an annotation like @RestController, @ResponseBody and @GetMapping in Grails or can they be used in Grails too, since Grails 2.4.5 is built on top of Spring?
**/

/**
** I used just one of the multiple domain/entity classes as a parameter to the parent constructor,
** in order to follow that convention like this below.
**/
   ClassNameController(){
       super(OneDomainClassOutOfTheManyDomainClassesInsideDTOClass)
   }


List<ClassDTO> getAllDTOs(){
     //allClassDTOs() is a private method inside same controller returning a list of all classDTOs
     List<ClassDTO> classDTOsList = allClassDTOs() 
     respond classDTOsList, status: 200
}

With that code, I can see the data displayed in Postman as well as inside the browser. However, I am not able to see the data when testing with CUrl, somehow looking at the log, Grails considers the request as successful and redirect to the home page. I am not sure why that's the case. I even removed any security to see if it was due to the Oauth Access token, but I can see that it's not the case, as even with @Secured(['IS_AUTHENTICATED_FULLY']), I can see in the logs of Spring Security that the user has been authenticated successfully and the authorization access has been granted successfully.

Pretty much, I am looking for a sample example of a simple Grails REST API that doesn't use the Grails convention and returns a non domain object.

0 Answers
Related