我正在使用一个开源项目将对象转换为Json,它为我提供了处理字段排除的工具。 我的servlet.xml代码片段具有相同的外观
<bean id="jsonViewSupport" class="com.monitorjbl.json.JsonViewSupportFactoryBean" />
JsonViewSupportFactoryBean类具有如下方法
/**
* Registering custom serializer allows to the JSonView to deal with custom serializations for certains field types.<br>
* This way you could register for instance a JODA serialization as a DateTimeSerializer. <br>
* Thus, when JSonView find a field of that type (DateTime), it will delegate the serialization to the serializer specified.<br>
* @param <T> Type class of the serializer
* @param cls {@link Class} the class type you want to add a custom serializer
* @param forType {@link JsonSerializer} the serializer you want to apply for that type
*/
public <T> void registerCustomSerializer( Class<T> cls, JsonSerializer<T> forType )
{
this.converter.registerCustomSerializer( cls, forType );
}
我有一个DateMapper类,如下所示
public class DateMapper extends JsonSerializer<Date>{
@Override
public void serialize(Date dateObje, JsonGenerator jgen, SerializerProvider serializers) throws IOException, JsonProcessingException {
// TODO Auto-generated method stub
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
jgen.writeString(dateFormat.format(dateObje));
}
}
我不确定如何调用此方法,并从servlet.xml中为Date对象设置自定义序列化器。
感谢任何帮助,感谢