MongoDB SpringBoot ObjectId序列化json为String

 mongodb的ObjectId默认序列化为bean对象,如果需要转换为json字符对象,配置如下

@Configuration
public class JacksonConfig implements InitializingBean {

    @Resource
    private ObjectMapper objectMapper;

    @Override
    public void afterPropertiesSet() {
        SimpleModule simpleModule = new SimpleModule();
        //指定mongodb ObjectId 序列化方式
        simpleModule.addSerializer(ObjectId.class, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
    }
}

 

posted @ 2021-05-22 15:36  茅坤宝骏氹  阅读(12)  评论(0)    收藏  举报  来源