gpt4 book ai didi

java - 将 Decimal128 序列化为 JSON

转载 作者:行者123 更新时间:2023-12-01 22:15:02 47 4
gpt4 key购买 nike

我正在使用 Spring Boot 创建一个 api,在其中从 mongodb 检索值。我面临的问题是 Decimal128 字段序列化如下:

"userid": {
"high": 3476778912330022912,
"low": 10776,
"naN": false,
"infinite": false,
"finite": true,
"negative": false
}

这是我的 Controller 的主体:

var wrappers = new EmbeddedWrappers(false);

var collection = mongoTemplate.getCollection("blah");

var result = collection.find().limit(1000).into(new ArrayList<>());

if (result == null) {
return ResponseEntity.notFound().build();
}

return ResponseEntity.ok(new Resources<>(Arrays.asList(wrappers.wrap(result))));

有什么想法可以正确序列化 Decimal128 值,以便我只拥有该值本身吗?

最佳答案

根据评论:

@Configuration
public class ObjectMapperConfig {

@Bean
public ObjectMapper objectMapper() {
var mapper = new ObjectMapper();

var module = new SimpleModule();
module.addSerializer(Decimal128.class, new Decimal128Serializer());
mapper.registerModule(module);

return mapper;
}

private static class Decimal128Serializer extends JsonSerializer<Decimal128> {
@Override
public void serialize(Decimal128 v, JsonGenerator g, SerializerProvider p) throws IOException {
g.writeNumber(v.bigDecimalValue());
}
}
}

关于java - 将 Decimal128 序列化为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58633942/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com