Java Web法式中應用Spring框架前往JSON格局的日期。本站提示廣大學習愛好者:(Java Web法式中應用Spring框架前往JSON格局的日期)文章只能為提供參考,不一定能成為您想要的結果。以下是Java Web法式中應用Spring框架前往JSON格局的日期正文
前往Json時格局化日期Date
第一步:創立CustomObjectMapper類
/** * 處理SpringMVC應用@ResponseBody前往json時,日期格局默許顯示為時光戳的成績。需合營<mvc:message-converters>應用 */ @Component("customObjectMapper") public class CustomObjectMapper extends ObjectMapper { public CustomObjectMapper() { CustomSerializerFactory factory = new CustomSerializerFactory(); factory.addGenericMapping(Date.class, new JsonSerializer<Date>() { @Override public void serialize(Date value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); jsonGenerator.writeString(sdf.format(value)); } }); this.setSerializerFactory(factory); } }
第二步:設置裝備擺設以下:
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="objectMapper" ref="customObjectMapper"></property> </bean> </mvc:message-converters> </mvc:annotation-driven>
後果以下:
格局化前
格局化後
進階:前往自界說格局日期
應用@ResponseBody時前往json字符串的日期格局。Date類型屬性默許前往一個Long型的時光戳,如何可以或許前往自界說的日期格局?
處理計劃:今朝有兩種方法完成,
1、部分修正(網上較多,但不推舉);
繼續Jackson的籠統類:JsonSerializer<T>,然後在javabean的屬性getter()上添加注解@JsonSerialize便可完成。
代碼以下:
import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonProcessingException; import org.codehaus.jackson.map.JsonSerializer; import org.codehaus.jackson.map.SerializerProvider; /** * @description 自界說前往JSON 數據格中日期格局化處置 */ public class CustomDateSerializer extends JsonSerializer<Date> { @Override public void serialize(Date value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); jsonGenerator.writeString(sdf.format(value)); } }
應用方法:
@JsonSerialize(using = CustomDateSerializer.class) public Date getCreateDate() { return createDate; }
2、全局修正(激烈推舉):
MappingJacksonHttpMessageConverter重要經由過程ObjectMapper來完成前往json字符串。這裡我們繼續該類,注冊一個JsonSerializer<T>。然後在設置裝備擺設文件中注入自界說的ObjectMapper。
代碼以下:
import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonProcessingException; import org.codehaus.jackson.map.JsonSerializer; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.SerializerProvider; import org.codehaus.jackson.map.ser.CustomSerializerFactory; /** * @description 處理Date類型前往json格局為自界說格局 */ public class CustomObjectMapper extends ObjectMapper { public CustomObjectMapper(){ CustomSerializerFactory factory = new CustomSerializerFactory(); factory.addGenericMapping(Date.class, new JsonSerializer<Date>(){ @Override public void serialize(Date value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); jsonGenerator.writeString(sdf.format(value)); } }); this.setSerializerFactory(factory); } }
spring-servlet.xml中設置裝備擺設:
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="objectMapper" ref="customObjectMapper"></property> </bean> </mvc:message-converters> </mvc:annotation-driven> <bean id="customObjectMapper" class="com.pmc.dwa.common.custom.CustomObjectMapper"></bean>