1
兩個自定義日期串行:如何在以下情況下選擇模型的特定字段的自定義serilaizer低於
package test;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class DateFormatter extends StdSerializer<Date> {
private SimpleDateFormat sampleFormatOne= new SimpleDateFormat("dd-MMM-yyyy");
public DateFormatter(Class<Date> t) {
super(t);
}
public DateFormatter() {
super(Date.class);
}
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException,
JsonGenerationException {
java.util.Date inputDate = new java.util.Date(date.getTime());
jsonGenerator.writeString(sampleFormatOne.format(inputDate));
}
}
也有是日期另一個串行:
package test;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class DateFormatterTwo extends StdSerializer<Date> {
private SimpleDateFormat samoelFormatTwo= new SimpleDateFormat("dd-mm-yyyy");y");
public DateFormatterTwo (Class<Date> t) {
super(t);
}
public DateFormatterTwo() {
super(Date.class);
}
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException,
JsonGenerationException {
java.util.Date inputDate = new java.util.Date(date.getTime());
jsonGenerator.writeString(samoelFormatTwo.format(inputDate));
}
}
Spring配置是如下:
<beans:bean id="customDate"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<beans:property name="objectMapper">
<beans:bean
class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<beans:property name="serializers">
<beans:array>
<beans:bean class="test.DateFormatter" />
<beans:bean class="test.DateFormatterTwo" />
</beans:array>
</beans:property>
</beans:bean>
</beans:property>
</beans:bean>
<beans:bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter" />
<beans:ref bean="customDate" />
</beans:list>
</beans:property>
</beans:bean>
現在我需要做的是建立一個JSON對象基於依賴於域值employeeType的值f irst or second formatter
,做日期的序列化。
隨着當前實現,我可以生成固定格式首先一個JSON。 如何在運行時基於另一個字段值選擇序列化程序。 ?
以下是模型
private field String employeeType;
@JsonProperty("empJoiningDate")
@JsonSerialize(using=DateFormatter.class)
private Date empJoiningDate = null;
感謝輸入,讓我試試看,現在有一個問題,我在哪裏註釋「@JsonSerialize(using = EmployeeSerializer .class)」?上課代替現場?感謝您的信息@tmarwen –
嘿Sanyam,因爲你已經猜到它應該是其應註明Employee類。 我已經更新了相應的答案以提供完整的配置說明。如果它適合您的需求,請毫不猶豫地接受並提出答案。 – tmarwen
嘿,這工作得很好非常感謝:)你也可以給予好評我的問題,如果你喜歡它 –