有没有办法为不同的目的创建两个 ObjectMapper 实例。
修改后的 ObjectMapper
@Component
class MyObjectMapper extends ObjectMapper{
public MyObjectMapper(){
super();
}
public MyObjectMapper(MyObjectMapper om) {
super(om);
}
@Override
public MyObjectMapper copy() {
return new MyObjectMapper(this);
}
}
现在使用如下
@Autowired ObjectMapper objectMapper; //performs standard serialization
@Autowire MyObjectMapper myMapper; //i can add custom filters in thiis mapper.
我尝试了类似的设置,但自定义映射器实际上影响了原始映射器,所有 rest controllers
抛出 JSON 解析错误:无法识别的字段
编辑: 不确定这一点是否很重要但仍然添加它
我正在使用 spring-boot-starter-json
最佳答案
这正是您应该使用 @Qualifier
的地方注释。
This annotation may be used on a field or parameter as a qualifier for candidate beans when autowiring. It may also be used to annotate other custom annotations that can then in turn be used as qualifiers.
关于java - ObjectMapper 的两个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58418456/