SpringBoot 2.2.2: Jackson serializer for custom pagination doesn't work

MacFly3181 :

Since SpringBoot 2.2.2, the custom pagination serializer with Jackson (2.10.1) doesn't work and isn't executed when serializing.

/**
 * This class allows to specify configuration related to the Web MVC part.
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    private static final String JSON_DATA_PROPERTY = "data";

    /**
     * Allows to configure a {@link JsonSerializer} for pagination.
     *
     * @return an instance of {@link Module}.
     */
    @SuppressWarnings("rawtypes")
    @Bean
    public Module springDataPageModule() {
        return new SimpleModule().addSerializer(Page.class, new JsonSerializer<Page>() {
            @Override
            public void serialize(final Page page, final JsonGenerator jsonGenerator,
                    final SerializerProvider serializers) throws IOException {

                jsonGenerator.writeStartObject();
                jsonGenerator.writeObjectField(JSON_DATA_PROPERTY, page.getContent());
                jsonGenerator.writeObjectFieldStart("paging");
                jsonGenerator.writeNumberField("page", page.getNumber() + 1);
                jsonGenerator.writeNumberField("totalPages", page.getTotalPages());
                jsonGenerator.writeNumberField("totalElements", page.getTotalElements());
                jsonGenerator.writeNumberField("perPage", page.getSize());
                jsonGenerator.writeEndObject();
                jsonGenerator.writeEndObject();
            }
        });
    }
...
}

With SpringBoot 2.2.1, this custom pagination serializer is applied and works. Can you see this problem ?

MacFly3181 :

The behavior has changed since SpringBoot 2.2.2. You have to go through the registration of the module

/**
 * This class allows to specify configuration related to the Web MVC part.
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    private static final String JSON_DATA_PROPERTY = "data";

    /**
     * Allows to configure a {@link JsonSerializer} for pagination.
     *
     * @return an instance of {@link Module}.
     */
    private Module preparePageModule() {
        return new SimpleModule().addSerializer(Page.class, new JsonSerializer<>() {
            @Override
            public void serialize(@SuppressWarnings("rawtypes") final Page page, final JsonGenerator jsonGenerator,
                    final SerializerProvider serializers) throws IOException {

                jsonGenerator.writeStartObject();
                jsonGenerator.writeObjectField(JSON_DATA_PROPERTY, page.getContent());
                jsonGenerator.writeObjectFieldStart("paging");
                jsonGenerator.writeNumberField("page", page.getNumber() + 1);
                jsonGenerator.writeNumberField("totalPages", page.getTotalPages());
                jsonGenerator.writeNumberField("totalElements", page.getTotalElements());
                jsonGenerator.writeNumberField("perPage", page.getSize());
                jsonGenerator.writeEndObject();
                jsonGenerator.writeEndObject();
            }
        });
    }

    /**
     * Allows to configure the Jackson object mapper.
     *
     * @param objectMapper
     *            an instance of {@link ObjectMapper}.
     */
    @Autowired
    public void configureJacksonObjectMapper(final ObjectMapper objectMapper) {
        ...
        objectMapper.registerModule(preparePageModule());
    }
...
}
`

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=357398&siteId=1