How to limit the visibility of domain properties in grails?

Is there any recommended way to limit domain visibility in grails?

Usually you do something like getting an interface for external use:

def productList = Product.list() withFormat { html {[productList:productList]} json { render productList as JSON } xml { render productList as XML } rss { render(feedType:"rss", productList)} } 

which is equal

 SELECT * FROM product 

But by default there are properties in the domain that should not be populated. Therefore i need to say something

 SELECT id, name, foo1, foo2 FROM product 

therefore, only a list of properties is included in the response.

+2
source share
1 answer

You can use the second class of the domain class as a view. The trick is to set up the mapping so that it has the same table as the Product class:

 class ProductView { String name Foo foo1 Foo foo2 static mapping = { table 'product' } } 

Then use this in your interface:

 def productList = ProductView.list() withFormat { html {[productList:productList]} json { render productList as JSON } xml { render productList as XML } rss { render(feedType:"rss", productList)} } 
+2
source

Source: https://habr.com/ru/post/1309761/


All Articles