zutnop / telekom-workflow-engine

An embeddable Java framework for running long-lived business processes
MIT License
27 stars 14 forks source link

telekom-workflow-example with Spring 4 uses GsonHttpMessageConverter, which by default doesn't serialize NULL values #8

Open zutnop opened 7 years ago

zutnop commented 7 years ago

For example, when looking at workflow instances list, where a workflow instance's label2 value is null, browser will alert: "DataTables warning: table id=instancesTable - Requested unknown parameter 'label2' for row 0. For more information about this error, please see http://datatables.net/tn/4"

As the telekom-workflow-example runtime environment contains a GSON library (and doesn't contain Jackson libraries) then Spring MVC 4 configures the default GsonHttpMessageConverter, which contains a default Gson instance, where serializeNulls=false. Thus all null values are stripped from the response JSON.

It's not affecting our real application, because we use Jackson libraries and thus Spring uses Jackson message converter.

CORRECT FIX: 1) Remove Spring 3 support classes, like GsonHttpMessageConverterForSpring3. 2) Re-configure (or plug in a new instance) the Springs message converter. One way:

@Configuration
class WebMvcConfiguration extends WebMvcConfigurationSupport {
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        for (HttpMessageConverter converter : converters) {
            if (converter instanceof GsonHttpMessageConverter) {
                Gson gson = new GsonBuilder().serializeNulls().create();
                ((GsonHttpMessageConverter)converter).setGson(gson);
            }
        }
    }
}