第一步:創建CustomObjectMapper類
- /**
- * 解決SpringMVC使用@ResponseBody返回json時,日期格式默認顯示爲時間戳的問題。需配合<mvc:message-converters>使用
- *
- * @author hellostory
- * @date 2013-10-31 下午04:17:52
- */
- @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>
效果如下:
格式化前:
格式化後:
又到搭新開發環境的時候,總是不免去網上搜下目前最新的框架。spring是web開發必用的框架,於是乎下載了目前最新的spring4.0.3,同時越來越不想用struts2,想試試spring mvc,也將spring-webmvc4.0.3下了下來,投入兩天時間學習後,發現還是挺優雅的,特別是從3.0後,spring mvc使用註解方式配製,以及對rest風格的支持,真是完美致極。
下面將這兩天研究到的問題做個總結,供參考。
1.request對象的獲取
方式1:在controller方法上加入request參數,spring會自動注入,如:public String list(HttpServletRequest request,HttpServletResponse response)
方式2:在controller類中加入@Resource private HttpServletRequest request 屬性,spring會自動注入,這樣不知道會不會出現線程問題,因爲一個controller實例會爲多個請求服務,暫未測試。
方式3:在controller方法中直接寫代碼獲取 HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
方式4:在controller中加入以下方法,此方法會在執行此controller的處理方法之前執行
- @ModelAttribute
- private void initServlet(HttpServletRequest request,HttpServletResponse response) {
- //String p=request.getParameter("p");
- //this.req=request;//實例變量,有線程安全問題,可以使用ThreadLocal模式保存
- }
2.response對象的獲取
可以參照以上request的獲取方式1和方式4,方式2和方式3對response對象無效!3.表單提交之數據填充
直接在方法上加入實體對象參數,spring會自動填充對象中的屬性,對象屬性名要與<input>的name一致纔會填充,如:public boolean doAdd(Demo demo)
4.表單提交之數據轉換-Date類型在實體類的屬性或get方法上加入 @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss"),那麼表單中的日期字符串就會正確的轉換爲Date類型了。還有@NumberFormat註解,暫時沒用,就不介紹了,一看就知道是對數字轉換用的。
5.json數據返回在方法上加入@ResponseBody,同時方法返回值爲實體對象,spring會自動將對象轉換爲json格式,並返回到客戶端。如下所示:
- @RequestMapping("/json1")
- @ResponseBody
- public Demo json1() {
- Demo demo=new Demo();
- demo.setBirthday(new Date());
- demo.setCreateTime(new Date());
- demo.setHeight(170);
- demo.setName("tomcat");
- demo.setRemark("json測試");
- demo.setStatus((short)1);
- return demo;
- }
這種方式是spring提供的,我們還可以自定義輸出json,以上第二條不是說了獲取response對象嗎,拿到response對象後,任由開發人員宰割,想怎麼返回就怎麼返回。
方法不要有返回值,如下:
- @RequestMapping("/json2")
- public void json2() {
- Demo demo=new Demo();
- demo.setBirthday(new Date());
- demo.setCreateTime(new Date());
- demo.setHeight(170);
- demo.setName("tomcat");
- demo.setRemark("json測試");
- demo.setStatus((short)1);
- String json=JsonUtil.toJson(obj);//;json處理工具類
- HttpServletResponse response = //獲取response對象
- response.getWriter().print(json);
- }
先寫一個日期轉換器,如下:
- public class JsonDateSerializer extends JsonSerializer<Date> {
- private SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- @Override
- public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
- throws IOException, JsonProcessingException {
- String value = dateFormat.format(date);
- gen.writeString(value);
- }
- }
- @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
- @JsonSerialize(using=JsonDateSerializer.class)
- public Date getCreateTime() {
- return this.createTime;
- }
你真的滿意了嗎,這麼不優雅的解決方案,假設birthday屬性是這樣的,只有年月日,無時分秒
- @DateTimeFormat(pattern="yyyy-MM-dd")
- public Date getBirthday() {
- return this.birthday;
- }
- @DateTimeFormat(pattern="yyyy-MM-dd")
- @JsonSerialize(using=JsonDate2Serializer.class)
- public Date getBirthday() {
- return this.birthday;
- }
經過分析源碼,找到一個不錯的方案,此方案將不再使用@JsonSerialize,而只利用@DateTimeFormat配置日期格式,jackson就可以正確轉換,但@DateTimeFormat只能配置在get方法上,這也沒什麼關係。
先引入以下類,此類對jackson的ObjectMapper類做了註解掃描攔截,使它也能對加了@DateTimeFormat的get方法應用日期格式化規則
- package com.xxx.utils;
- import java.io.IOException;
- import java.lang.reflect.AnnotatedElement;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.springframework.format.annotation.DateTimeFormat;
- import org.springframework.stereotype.Component;
- import com.fasterxml.jackson.core.JsonGenerator;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.JsonSerializer;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.databind.SerializerProvider;
- import com.fasterxml.jackson.databind.introspect.Annotated;
- import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
- import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
- /**
- * json處理工具類
- * @author zhangle
- */
- @Component
- public class JsonUtil {
- private static final String DEFAULT_DATE_FORMAT="yyyy-MM-dd HH:mm:ss";
- private static final ObjectMapper mapper;
- public ObjectMapper getMapper() {
- return mapper;
- }
- static {
- SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
- mapper = new ObjectMapper();
- mapper.setDateFormat(dateFormat);
- mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
- @Override
- public Object findSerializer(Annotated a) {
- if(a instanceof AnnotatedMethod) {
- AnnotatedElement m=a.getAnnotated();
- DateTimeFormat an=m.getAnnotation(DateTimeFormat.class);
- if(an!=null) {
- if(!DEFAULT_DATE_FORMAT.equals(an.pattern())) {
- return new JsonDateSerializer(an.pattern());
- }
- }
- }
- return super.findSerializer(a);
- }
- });
- }
- public static String toJson(Object obj) {
- try {
- return mapper.writeValueAsString(obj);
- } catch (Exception e) {
- throw new RuntimeException("轉換json字符失敗!");
- }
- }
- public <T> T toObject(String json,Class<T> clazz) {
- try {
- return mapper.readValue(json, clazz);
- } catch (IOException e) {
- throw new RuntimeException("將json字符轉換爲對象時失敗!");
- }
- }
- public static class JsonDateSerializer extends JsonSerializer<Date>{
- private SimpleDateFormat dateFormat;
- public JsonDateSerializer(String format) {
- dateFormat = new SimpleDateFormat(format);
- }
- @Override
- public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
- throws IOException, JsonProcessingException {
- String value = dateFormat.format(date);
- gen.writeString(value);
- }
- }
- }
- <mvc:annotation-driven>
- <mvc:message-converters>
- <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
- <property name="objectMapper" value="#{jsonUtil.mapper}"/>
- <property name="supportedMediaTypes">
- <list>
- <value>text/json;charset=UTF-8</value>
- </list>
- </property>
- </bean>
- </mvc:message-converters>
- </mvc:annotation-driven>
接下來就可以這樣配置實體類,jackson也能正確轉換Date類型
- @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
- public Date getCreateTime() {
- return this.createTime;
- }
- @DateTimeFormat(pattern="yyyy-MM-dd")
- public Date getBirthday() {
- return this.birthday;
- }
完畢,一切都完美了。
以下爲2014/4/21 補充
寫了那麼多,發現白忙活了一場,原來jackson也有一個@JsonFormat註解,將它配置到Date類型的get方法上後,jackson就會按照配置的格式轉換日期類型,而不自定義轉換器類,欲哭無淚啊。辛苦了那麼多,其實別人早已提供,只是沒有發現而已。
不說了,直接上方案吧。
1.spring配置照樣是這樣:
- <mvc:annotation-driven>
2.JsonUtil可以不用了,但如果要自己從response對象輸出json,那麼還是可以用,但改成了這樣
- package com.xxx.utils;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import org.springframework.stereotype.Component;
- import com.fasterxml.jackson.databind.ObjectMapper;
- /**
- * json處理工具類
- * @author zhangle
- */
- @Component
- public class JsonUtil {
- private static final String DEFAULT_DATE_FORMAT="yyyy-MM-dd HH:mm:ss";
- private static final ObjectMapper mapper;
- static {
- SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
- mapper = new ObjectMapper();
- mapper.setDateFormat(dateFormat);
- }
- public static String toJson(Object obj) {
- try {
- return mapper.writeValueAsString(obj);
- } catch (Exception e) {
- throw new RuntimeException("轉換json字符失敗!");
- }
- }
- public <t> T toObject(String json,Class<t> clazz) {
- try {
- return mapper.readValue(json, clazz);
- } catch (IOException e) {
- throw new RuntimeException("將json字符轉換爲對象時失敗!");
- }
- }
- }</t></t>
3.實體類的get方法就需要多一個@JsonFormat的註解配置
- @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
- @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
- public Date getCreateTime() {
- return this.createTime;
- }
- @DateTimeFormat(pattern="yyyy-MM-dd")
- @JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
- public Date getBirthday() {
- return this.birthday;
- }