API / Apache FreeMarker Interview questions
Why should you use an ObjectWrapper such as DefaultObjectWrapper in FreeMarker?
Templates only ever interact with FreeMarker's own template model interfaces - scalars, sequences, hashes, and so on - not with arbitrary Java classes directly. An ObjectWrapper is the adapter that sits between the two: it takes whatever Java object the data model contains and exposes it through the right template model interface, so ${user.name} can transparently call user.getName(), and a Java List can transparently be iterated with <#list>.
Without a properly configured object wrapper, a template author would have no consistent, safe way to reach into POJOs, maps, and collections; the wrapper is also the layer that decides which classes and methods are visible at all, which is central to keeping template execution predictable and reasonably sandboxed. Configuration.getObjectWrapper() defaults to a sensible general-purpose wrapper, but it is commonly set explicitly to new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_32).build() or similar to pin the exact behavior an application relies on.
More Related questions...