开发者

How to exclude params from XML rendered in Grails?

I have a class :

class Category {
  String name
  SortedSet items
  static hasMany = [items:Item]
}

Inside the controller, I render category as XML (converters) :

 开发者_如何学C  def getCategory = {
     render Category.read(1) as XML
   }

But I want exclude items from the rendering.

How can I do ?

Thanks


You can simply return a Map with only the properties you want to include:

def getCategory = {
    def category = Category.read(1)
    render [ id: category.id, name: category.name ] as XML
}


You need to create a second domain class sort of like a view and configure the mapping so it has the same table as the Category class

See the entire answer on this thread : How to restrict visibility of domain properties in grails?

Fabien.


Another option is to use the marshallers plugin. It lets you define a custom marshaller either on the object itself or elsewhere. For example:

class Category {
  String name
  SortedSet items
  static hasMany = [items:Item]
  static marshallers {
    shouldOutputIdentifier false
    shouldOutputVersion false
    shouldOutputClass false
    elementName 'category'
    attribute 'name'
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜