gpt4 book ai didi

java - 如何使用 Jackson 将嵌套的 ObjectId 序列化为 String?

转载 作者:行者123 更新时间:2023-11-30 08:03:03 26 4
gpt4 key购买 nike

many questions关于从 ObjectId 的转换至 String与 jackson 。所有答案都建议创建自己的 JsonSerializer<ObjectId>或注释 ObjectId字段 @JsonSerialize(using = ToStringSerializer.class) .

但是,我有一张 map 有时包含ObjectIds ,即:

class Whatever {
private Map<String, Object> parameters = new HashMap<>();
Whatever() {
parameters.put("tom", "Cat");
parameters.put("jerry", new ObjectId());
}
}

我希望 jackson 将其转换为:

{
"parameters": {
"tom": "cat",
"jerry": "57076a6ed1c5d61930a238c5"
}
}

但是我得到:

{
"parameters": {
"tom": "cat",
"jerry": {
"date": 1460103790000,
"machineIdentifier": 13747670,
"processIdentifier": 6448,
"counter": 10631365,
"time": 1460103790000,
"timestamp": 1460103790,
"timeSecond": 1460103790
}
}
}

我已经用

注册了转换(在 Spring 中)
public class WebappConfig extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder
.serializerByType(ObjectId.class, new ToStringSerializer());
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(builder.build());
converters.add(converter);
}
}

和一级ObjectIds被正确转换。如何让 jackson 也转换嵌套的?我是否必须为此 map 编写自定义转换器?

请记住,这 Map可以嵌套多次(即包含另一个 map )。我只想转换 ObjectIdString每当 jackson 看到它。

最佳答案

我想您正在从 org.springframework.boot:spring-boot-starter-data-mongodb 获取 org.bson.types.ObjectId。你的代码对我来说工作得很好。我能看到的一件事是您没有在 WebappConfig 上方显示 @Configuration 注释。

这是我的演示项目,你能在你的设置上测试一下吗?

应用程序.java

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.bson.types.ObjectId;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Configuration
public static class WebappConfig extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder
.serializerByType(ObjectId.class, new ToStringSerializer());
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(builder.build());
converters.add(converter);
}
}

@RestController
public static class MyRestController {

@ResponseBody
@RequestMapping("/")
public Whatever method() {
return new Whatever();
}
}

public static class Whatever {
private Map<String, Object> parameters = new HashMap<>();

public Whatever() {
parameters.put("tom", "Cat");
parameters.put("jerry", new ObjectId());
}

public Map<String, Object> getParameters() {
return parameters;
}

public void setParameters(Map<String, Object> parameters) {
this.parameters = parameters;
}
}
}

来自 127.0.0.1:8080 的响应

{
"parameters": {
"tom": "Cat",
"jerry": "5709df1cf0d9550b4de619d2"
}
}

Gradle :

dependencies {
compile("org.springframework.boot:spring-boot-starter-data-mongodb")
compile("org.springframework.boot:spring-boot-starter-web")
}

关于java - 如何使用 Jackson 将嵌套的 ObjectId 序列化为 String?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36494953/

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