Reputation: 7474
I currently define custom API responses for each resource using
JSON.registerObjectMarshaller(Something) { ... }
inside the BootStrap init
closure.
Using render something as JSON
gives me the desired output. Some properties of the domain class are filtered out according to the marshaller setup.
However, when I use the respond method, it does not use the marshaller format, but instead displays all of the properties of the domain class.
Is there any way to make respond use the desired output set up by the marshaller?
Grails version: 3.2.8
Update:
It seems the problem arises when using the rest-api profile.
Upvotes: 4
Views: 1067
Reputation: 2897
This problem happens when you create grails application using profile: rest-api.
More precisely, the problem is caused by 2 conditions:
If you dive into 'respond', you will find:
renderer = registry.findRenderer(mimeType, value)
At this point, concrete class of renderer(Renderer) instance depends on whether conditions above are fulfilled or not.
(If you defined custom json renderer class for some class, that renderer would be returned here maybe. Custom json renderer is totally different from custom marshaller , so please ignore the case here.)
When renderer == DefaultJsonRenderer, rendering logic finally goes to:
protected void renderJson(T object, RenderContext context) {
JSON converter
if (namedConfiguration) {
JSON.use(namedConfiguration) {
converter = object as JSON
}
} else {
converter = object as JSON
}
renderJson(converter, context)
}
so, this lead to the same result as 'render --- as JSON'.
On the other hand, when renderer == JsonViewJsonRenderer,
it does not refer to custom registered marshaller created by JSON.registerObjectMarshaller
, but refers to gson view.
So, if you want to enable custom marshaller with keeping gson view functionality,
quick solution is, to remove _object.gson.
If you removed views-json plugin, no one can implement gson view in the application, which may cause inconvenience in the future.
Upvotes: 4