java - Spring RestController custom view resolver -
i've started implementing application using spring, , particularly making use of @restcontroller
annotations. i've defined custom view resolver before normal controllers, i'm wondering how similar rest controller. i'm trying capture json before gets returned can pretty print it.
i guess since it's not routing "view" view resolver wrong approach. should using? can use handlerinterceptor
perhaps? how target @restcontroller
annotated classes?
your controller (most likely) ends returning object gets serialized json appropriate httpmessageconverter
-- this one.
as message converter writes json representation of returned object directly response output stream, need subclass mappingjackson2httpmessageconverter
, overwrite writeinternal
method chance apply custom logic around serialization. you'll need register custom converter instead of default 1 (or @ least before it).
if post-processing can replaced customized serialization can handled jackson, best choice set objectmapper
appropriately , pass jackson message converter @ construction time.
i don't think interceptor can you, provides modelandview
containing object returned controller, not serialized version.
as alternative, can of course use request filter (outside whole spring mvc lifecycle), provide custom http response wrapper (with bytearrayoutputstream
accumulate json output) , post-process before writing real response output stream.
Comments
Post a Comment