【发布时间】:2016-09-30 13:25:09
【问题描述】:
我正在开发一个 java spring mvc 项目。我创建了 CustomObjectMapper 类,该类扩展了 ObjectMapper 形式的 jackson。我还在弹簧配置中设置了CustomObjectMapper,所以每次杰克逊想要serialize 或deserialize,我的CustomObjectMapper 工作,一切正常。但是我有一个问题:
我创建了一个自定义注释@AllowHtml,并将它放在我模型中的一些String 字段之上。我也以这种方式创建了一个JsonDeserializerString 类:
public class JsonDeserializerString extends JsonDeserializer<String>{
@Override
public String deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException {
return jp.getText();
}
}
我以这种方式在我的CustomObjectMapper 中设置了这个反序列化器:
@Component
public class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper(){
SimpleModule module = new SimpleModule();
module.addDeserializer(String.class, new JsonDeserializerString());
this.registerModule(module);
}
}
这按预期工作,当用户提交表单时,每个字符串字段都会使用 JsonDeserializerString 反序列化。但我想在反序列化器中获取字段注释。。其实我想,如果一个字符串字段在模型中有一定的注解,做一些逻辑。我该怎么做?
【问题讨论】:
标签: java json spring-mvc jackson deserialization