由于SpringBoot 2.2.2,与杰克逊自定义分页串行器(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,这个自定义分页串行应用和工作。你可以看这个问题?
因为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());
}
...
}
`
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句