2017-06-05 36 views
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; 

回答

1

領域爲了有empJoiningDate場系列化與視employeeType的格式,你需要創建一個自定義SerializerEmployee POJO鍵入而不是Date字段。

按照這一辦法,你將有機會獲得所有的對象字段,並可以提供您的條件日期序列化邏輯。

Serializer實施將東西如下:

private static class EmployeeSerializer extends StdSerializer<Employee> { 

    private static final String TYPE_ONE = "SomeType"; 

    private static final String TYPE_TWO = "SomeOtherType"; 

    private SimpleDateFormat sampleFormatOne = /** format initialized **/; 

    private SimpleDateFormat sampelFormatTwo = /** format initialized **/; 

    protected EmployeeSerializer() { 
     super(Employee.class); 
    } 

    @Override 
    public void serialize(Employee employee, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { 
     jsonGenerator.writeStartObject(); 
     jsonGenerator.writeString(employee.getEmployeeType()); 
     switch (employee.getEmployeeType()) { 
      case TYPE_ONE: jsonGenerator.writeString(sampleFormatOne.format(employee.getEmpJoiningDate())); 
      case TYPE_TWO: jsonGenerator.writeString(sampelFormatTwo.format(employee.getEmpJoiningDate())); 
     } 
     jsonGenerator.writeEndObject(); 
    } 
} 

這樣,你會需要你的定製序列,而不是如日期字段來註釋Employee值類:

@JsonSerialize(using = EmployeeSerializer.class) 
public class Employee { 
    //... 
} 
+0

感謝輸入,讓我試試看,現在有一個問題,我在哪裏註釋「@JsonSerialize(using = EmployeeSerializer .class)」?上課代替現場?感謝您的信息@tmarwen –

+1

嘿Sanyam,因爲你已經猜到它應該是其應註明Employee類。 我已經更新了相應的答案以提供完整的配置說明。如果它適合您的需求,請毫不猶豫地接受並提出答案。 – tmarwen

+0

嘿,這工作得很好非常感謝:)你也可以給予好評我的問題,如果你喜歡它 –

相關問題