In many cases you might need just a subset of all model data. spring-json view provites some useful features you can use. The following Diagram explains the terms you going to meet in the following part.
The JsonViewFilter is designd for explecite exclusion of Model / CommandBean values and to provide flter capilities independent from the json-renderer.
It provides three functions:.
The SimplePropertyFilter is the default implementation of the JsonViewFilter interface.
It has three properties to declare the properties you want to exclude from the response. They correspond with the JsonViewFilter functions.
The Properties to exclude can be located in the command bean or Model map (starting with "(name_in_model_map_key))). The can be decalarated by an Commons BeanUtil like syntax in the spring configuration.
<beans> <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"> <property name="jsonViewFilter"><ref bean="jsonViewFilter"/></property> </bean> <bean name="jsonViewFilter" class="org.springframework.web.servlet.view.json.filter.SimplePropertyFilter"> <property name="excludeBeforePropNames"> <list> <!--ComandBean Values--> <value>name</value> <value>address.name</value> <!--ModelMap Values--> <value>(list)</value> <value>(countries).short</value> </list> </property> <property name="excludeBeforeErrorsPropNames"> <list> <value>successValues</value> </list> </property> <property name="excludeBeforeSuccessPropNames"> <list> <value>errorValues</value> </list> </property> </bean> </beans>
Finally you can create your own filter by implementing the JsonViewFilter interface.
public class CustomJsonFilter implements JsonViewFilter { // allways triggert. public void filterBeforePopulate(Map model, String commandName) throws Exception{ // Do something ... } // either ... triggered if no Error is added. public void filterBeforePopulateSuccess(Map model, String commandName) throws Exception{ // Do something else... } // or ... triggered if an Error is added. public void filterBeforePopulateErrors(Map model, String commandName) throws Exception{ // Do something else... } } <beans> <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"> <property name="jsonViewFilter"><ref bean="customJsonViewFilter"/></property> </bean> <bean name="customJsonViewFilter" class="org.springframework.web.servlet.view.json.filter.CustomJsonFilter"> </bean> </beans>
You can register a implementation of an JsonViewCallback to plugin custom behaviour. The "StandartErrorConverter" is the default JsonViewCallback of the spring-json view. It provides a filter feature in the case the BindingResult contains a Feld- or Globalerror.
There are 3 filtermodes: Set the modelCleanUpMode property in of the StandartErrorConverter in the spring-config.xml:
For further informations please see here.
You can create your own error / success handling by implementing the JsonViewCallback interface.
public class FilterJsonViewCallback implements JsonViewCallback { // either ... triggerd if a Global- or FieldError exists. public void public void onPopulateErrors( Map model, RequestContext rc, BindingResult br) throws Exception{ // remove or reset property in the Model } // or ... triggered if no Error is added. public void public void onPopulateSuccess( Map model, RequestContext rc, BindingResult br) throws Exception{ // remove or reset property in the Model } } <beans> <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"> <property name="jsonViewCallback"> <ref bean="jsonViewCallback" /> </property> </bean> <bean name="jsonViewCallback" class="org.springframework.web.servlet.view.json.error.FilterJsonViewCallback" /> </beans>
Each writer provides its own JsonWriterConfiguratorTemplate where you can register a custom writer configuration object. Here you can use the filter features of the specific json render library.
SojoJsonStringWriter | JsonlibJsonStringWriter | XStreamJsonStringWriter | |
spring-json Documentation | see | see | see |
API Documentation | see | see | xml annotations |
For code examples please see the JsonWriter documentation.