Filtering

  1. Instruction
  2. Recommended Filter Features
    1. JsonViewFilter
    2. Default JsonViewFilter - SimplePropertyFilter
    3. Custom implementaton of JsonViewFilter
  3. Other Filter Features
    1. StandartErrorConverter - The "model cleanup mode"
    2. Custom implementaton of JsonViewCallback
    3. Writer specific configuration via JsonWriterConfiguratorTemplates

Instruction

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.

Diagram
Errorhandling Process

Recommended Filter Feature

JsonViewFilter

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:.

  1. filterBeforePopulate : always executed
  2. filterBeforePopulateErrors : executed if the model contains errors
  3. filterBeforePopulateSuccess : executed if the model contains no errors

Default JsonViewFilter - SimplePropertyFilter

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.

  1. excludeBeforePropNames
  2. excludeBeforeErrorsPropNames
  3. excludeBeforeSuccessPropNames

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>

Custom implementaton of JsonViewFilter

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>

Other Filter Features

StandartErrorConverter - The "model cleanup mode"

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:

  1. KEEP_ALL : No values are deleted from the model
  2. REMOVE_COMMAND_BEAN : Just the Command bean is removed from the model
  3. KEEP_ERRORS_ONLY : Just the errors are kept. All other values of the model giong to be removed.

    For further informations please see here.

Custom implementaton of JsonViewCallback

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>

Writer specific configuration via JsonWriterConfiguratorTemplate

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.