spring mvc 使用及json 日期转换解决方案

 转:spring mvc 使用及json 日期转换解决方案

 

可以倒着看。。。。

第一步:创建CustomObjectMapper类

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. /**  
  2. <mvc:message-converters>  =  CustomSerializerFactory();  
  3. <Date>  =  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  4. }  


第二步:配置如下:

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <mvc:annotation-driven> <mvc:message-converters> <bean=> <property= =></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的处理方法之前执行

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. @ModelAttribute privatevoid   
  2. }  

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格式,并返回到客户端。如下所示:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. @RequestMapping"/json1" @ResponseBody public new new new );  
  2. );   
  3. );   
  4. short1 return }  

注 意:spring配置文件要加上:<mvc:annotation-driven/>,同时还要引入jackson- core.jar,jackson-databind.jar,jackson-annotations.jar(2.x的包)才会自动转换json
这种方式是spring提供的,我们还可以自定义输出json,以上第二条不是说了获取response对象吗,拿到response对象后,任由开发人员宰割,想怎么返回就怎么返回。
方法不要有返回值,如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. @RequestMapping"/json2" publicvoid new new new );  
  2. );  
  3. );  
  4. short1   
  5.   
  6. }  

OK,一切很 完美。接着恶心的问题迎面而来,date类型转换为json字符串时,返回的是long time值,如果你想返回“yyyy-MM-dd HH:mm:ss”格式的字符串,又要自定义了。我很奇怪,不是有@DateTimeFormat注解吗,为什么不利用它。难道 @DateTimeFormat只在表单提交时,将字符串转换为date类型,而date类型转换为json字符串时,就不用了。带着疑惑查源码,原来 spring使用jackson转换json字符,而@DateTimeFormat是spring-context包中的类,jackson如何转 换,spring不方便作过多干涉,于是只能遵守jackson的转换规则,自定义日期转换器。
先写一个日期转换器,如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. publicclassextends privatenew   
  2. publicvoid throws }  

在实体类的get方法上配置使用转换器,如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. @DateTimeFormat"yyyy-MM-dd HH:mm:ss" @JsonSerializeclass public returnthis }  

OK,到此搞定。
你真的满意了吗,这么不优雅的解决方案,假设birthday属性是这样的,只有年月日,无时分秒

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. @DateTimeFormat"yyyy-MM-dd" public returnthis }  

这意味着,又要为它定制一个JsonDate2Serializer的转换器,然后配置上,像这样

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. @DateTimeFormat"yyyy-MM-dd" @JsonSerializeclass public returnthis }  

假设还有其它格式的Date字段,还得要为它定制另一个转换器。my god,请饶恕我的罪过,不要让我那么难受
经过分析源码,找到一个不错的方案,此方案将不再使用@JsonSerialize,而只利用@DateTimeFormat配置日期格式,jackson就可以正确转换,但@DateTimeFormat只能配置在get方法上,这也没什么关系。
先引入以下类,此类对jackson的ObjectMapper类做了注解扫描拦截,使它也能对加了@DateTimeFormat的get方法应用日期格式化规则

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. package import import import import import import import import import import import import import import /** 
  2.  * json处理工具类 
  3.  * @author zhangle 
  4.  */ @Component publicclass privatestaticfinal privatestaticfinal public return static new new new   
  5. public ifinstanceof class ifnull if returnnew returnsuper publicstatic try return catch thrownew);  
  6. public try return catch thrownew);  
  7. publicstaticclassextends private public new   
  8. publicvoid throws }  

再 将<mvc:annotation-driven/>改为以下配置,配置一个新的json转换器,将它的ObjectMapper对象设置为 JsonUtil中的objectMapper对象,此转换器比spring内置的json转换器优先级更高,所以与json有关的转换,spring会 优先使用它。

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <mvc:annotation-driven> <mvc:message-converters> <bean=> <property= =/> <property=> <list> <value>=-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>  

接下来就可以这样配置实体类,jackson也能正确转换Date类型

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. @DateTimeFormat"yyyy-MM-dd HH:mm:ss" public returnthis @DateTimeFormat"yyyy-MM-dd" public returnthis }  

完毕,一切都完美了。


以下为2014/4/21 补充

写了那么多,发现白忙活了一场,原来jackson也有一个@JsonFormat注解,将它配置到Date类型的get方法上后,jackson就会按照配置的格式转换日期类型,而不自定义转换器类,欲哭无泪啊。辛苦了那么多,其实别人早已提供,只是没有发现而已。

不说了,直接上方案吧。

1.spring配置照样是这样:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. <mvc:annotation-driven>  

 

2.JsonUtil可以不用了,但如果要自己从response对象输出json,那么还是可以用,但改成了这样

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. package import import import import /** 
  2.  * json处理工具类 
  3.  * @author zhangle 
  4.  */ @Component publicclass privatestaticfinal privatestaticfinal static new new publicstatic try return catch thrownew);  
  5. public try return catch thrownew);  
  6. }</t></t>  

3.实体类的get方法就需要多一个@JsonFormat的注解配置

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
    1. @DateTimeFormat"yyyy-MM-dd HH:mm:ss" @JsonFormat"yyyy-MM-dd HH:mm:ss")  
    2. public returnthis @DateTimeFormat"yyyy-MM-dd" @JsonFormat"yyyy-MM-dd")  
    3. public returnthis
posted @ 2015-11-14 17:09  月是故乡明95  阅读(496)  评论(0)    收藏  举报