SpringBoot 2.2.2:用于自定义分页的Jackson序列化器不起作用

时间:2019-12-18 20:58:14

标签: java spring spring-boot serialization jackson

从SpringBoot 2.2.2开始,带有Jackson(2.10.1)的自定义分页序列化程序不起作用,并且在序列化时不执行。

/**
 * 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();
            }
        });
    }
...
}

在SpringBoot 2.2.1中,此自定义分页序列化程序已应用并可以工作。 你看到这个问题了吗?

1 个答案:

答案 0 :(得分:0)

自SpringBoot 2.2.2起,行为已更改。 您必须完成模块的注册

/**
 * 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());
    }
...
}
`