0

Working with grails 2.4.0 and RESTful controllers (JSON) and looking for guidance.

I have very simple domains (from a sample):

class Project {
    String name
    String description

    static belongsTo = [owner : EndUser]

    static constraints = {
        name(blank: false, unique: true)
        description()
    }
}


class EndUser {
    String userName
    String fullName

    String toString() {
        "${fullName}"
    }

    static hasMany = [projects : Project]

    static constraints = {
        fullName()      
        userName(unique: true)

    }

}

In ProjectController, I have kept html too for testing in addition to JSON.

class ProjectController extends RestfulController {
    static responseFormats = ['html', 'json', 'xml']

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond Project.list(params), model:[projectInstanceCount: Project.count()]
    }

    def show(Project projectInstance) {
        respond projectInstance
    }

    def listWithFullName(){
        def a = Project.list(params)
        a.owner.fullName
        respond a
    }
}

If go to html page: /project/show/1, I get html page with details:

HTML page:
Name: prj1
Description: prj desc
Owner: full name1

So far so good, if I do same with json (/project/show/1.json), I get:

{
class: "testapp.Project"
id: 1
description: "prj desc"
name: "prj1"
owner: {
    class: "testapp.EndUser"
    id: 2
 }
}

As you see for "owner" field in JSON I am getting "id". How can I get fullname for owner in JSON. I thought I can do some thing like what I attempted in controller in method: listWithFullName() but that did not work. Any pointers? Thanks.

user3741611
  • 327
  • 2
  • 16

0 Answers0