简体   繁体   中英

Grails Access JSON properties from request that don't map to specific Domain

Grails 2.4.2

I have a UserController and in its save method, I need to pass in an Organization ID along with the User data so that I can find the Organization and then create the many-to-many relationship. So I have something like this...

def save(User user) {
    def orgExternalId = request.JSON.orgExternalId
    userService.save(user, orgExternalId)
    // more boilerplate
}

I'm POST'ing the following json:

{ 
  "orgExternalId" : "123445", 
  "firstName": "gregg", 
  "lastName": "bolinger",
  "username": "gdboling"
}

However, I'm not getting the orgExternalId out of the JSON in my controller. Also, if I inspect the JSON, it is empty, but the User domain is populated correctly. My assumption at this point is I'm going to have to use a Command Object or I could put the orgExternalId in the header, but I'd like to know if there is another way.

Have a look at Binding The Request Body To Command Objects . Mainly:

Note that the body of the request is being parsed to make that work. Any attempt to read the body of the request after that will fail since the corresponding input stream will be empty. The controller action can either use a command object or it can parse the body of the request on its own (either directly, or by referring to something like request.JSON), but cannot do both.

I wasn't sure if I would find this verbiage while adding answer as a comment. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM