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

21 篇文章 0 订阅

又到搭新开发环境的时候,总是不免去网上搜下目前最新的框架。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 plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @ModelAttribute  
  2. private void initServlet(HttpServletRequest request,HttpServletResponse response) {  
  3.     //String p=request.getParameter("p");  
  4.     //this.req=request;//实例变量,有线程安全问题,可以使用ThreadLocal模式保存  
  5. }  

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 plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @RequestMapping("/json1")  
  2. @ResponseBody  
  3. public Demo json1() {  
  4.     Demo demo=new Demo();  
  5.     demo.setBirthday(new Date());  
  6.     demo.setCreateTime(new Date());  
  7.     demo.setHeight(170);  
  8.     demo.setName("tomcat");   
  9.     demo.setRemark("json测试");   
  10.     demo.setStatus((short)1);   
  11.     return demo;  
  12. }  
注意:spring配置文件要加上:<mvc:annotation-driven/>,同时还要引入jackson-core.jar,jackson-databind.jar,jackson-annotations.jar(2.x的包)才会自动转换json
这种方式是spring提供的,我们还可以自定义输出json,以上第二条不是说了获取response对象吗,拿到response对象后,任由开发人员宰割,想怎么返回就怎么返回。
方法不要有返回值,如下:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @RequestMapping("/json2")  
  2. public void json2() {  
  3.     Demo demo=new Demo();  
  4.     demo.setBirthday(new Date());  
  5.     demo.setCreateTime(new Date());  
  6.     demo.setHeight(170);  
  7.     demo.setName("tomcat");  
  8.     demo.setRemark("json测试");  
  9.     demo.setStatus((short)1);  
  10.     String json=JsonUtil.toJson(obj);//;json处理工具类  
  11.     HttpServletResponse response = //获取response对象  
  12.     response.getWriter().print(json);  
  13. }  
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 plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class JsonDateSerializer extends JsonSerializer<Date> {  
  2.    private SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  3.    @Override  
  4.    public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)  
  5.            throws IOException, JsonProcessingException {  
  6.        String value = dateFormat.format(date);  
  7.        gen.writeString(value);  
  8.    }  
  9. }  
在实体类的get方法上配置使用转换器,如下:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")  
  2. @JsonSerialize(using=JsonDateSerializer.class)  
  3. public Date getCreateTime() {  
  4.     return this.createTime;  
  5. }  
OK,到此搞定。
你真的满意了吗,这么不优雅的解决方案,假设birthday属性是这样的,只有年月日,无时分秒
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @DateTimeFormat(pattern="yyyy-MM-dd")  
  2. public Date getBirthday() {  
  3.     return this.birthday;  
  4. }  
这意味着,又要为它定制一个JsonDate 2 Serializer的转换器,然后配置上,像这样
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @DateTimeFormat(pattern="yyyy-MM-dd")  
  2. @JsonSerialize(using=JsonDate2Serializer.class)  
  3. public Date getBirthday() {  
  4.     return this.birthday;  
  5. }  
假设还有其它格式的Date字段,还得要为它定制另一个转换器。my god,请饶恕我的罪过,不要让我那么难受
经过分析源码,找到一个不错的方案,此方案将不再使用@JsonSerialize,而只利用@DateTimeFormat配置日期格式,jackson就可以正确转换,但@DateTimeFormat只能配置在get方法上,这也没什么关系。
先引入以下类,此类对jackson的ObjectMapper类做了注解扫描拦截,使它也能对加了@DateTimeFormat的get方法应用日期格式化规则
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.xxx.utils;  
  2.   
  3. import java.io.IOException;  
  4. import java.lang.reflect.AnnotatedElement;  
  5. import java.text.SimpleDateFormat;  
  6. import java.util.Date;  
  7. import org.springframework.format.annotation.DateTimeFormat;  
  8. import org.springframework.stereotype.Component;  
  9. import com.fasterxml.jackson.core.JsonGenerator;  
  10. import com.fasterxml.jackson.core.JsonProcessingException;  
  11. import com.fasterxml.jackson.databind.JsonSerializer;  
  12. import com.fasterxml.jackson.databind.ObjectMapper;  
  13. import com.fasterxml.jackson.databind.SerializerProvider;  
  14. import com.fasterxml.jackson.databind.introspect.Annotated;  
  15. import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;  
  16. import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;  
  17.   
  18. /** 
  19.  * json处理工具类 
  20.  * @author zhangle 
  21.  */  
  22. @Component  
  23. public class JsonUtil {  
  24.   
  25.         private static final String DEFAULT_DATE_FORMAT="yyyy-MM-dd HH:mm:ss";  
  26.         private static final ObjectMapper mapper;  
  27.           
  28.         public ObjectMapper getMapper() {  
  29.                 return mapper;  
  30.         }  
  31.   
  32.         static {  
  33.                 SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);  
  34.                   
  35.                 mapper = new ObjectMapper();  
  36.                 mapper.setDateFormat(dateFormat);  
  37.                 mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {  
  38.                         @Override  
  39.                         public Object findSerializer(Annotated a) {  
  40.                                 if(a instanceof AnnotatedMethod) {  
  41.                                         AnnotatedElement m=a.getAnnotated();  
  42.                                         DateTimeFormat an=m.getAnnotation(DateTimeFormat.class);  
  43.                                         if(an!=null) {  
  44.                                                 if(!DEFAULT_DATE_FORMAT.equals(an.pattern())) {  
  45.                                                         return new JsonDateSerializer(an.pattern());  
  46.                                                 }  
  47.                                         }  
  48.                                 }  
  49.                                 return super.findSerializer(a);  
  50.                         }  
  51.                 });  
  52.         }  
  53.           
  54.         public static String toJson(Object obj) {  
  55.                 try {  
  56.                         return mapper.writeValueAsString(obj);  
  57.                 } catch (Exception e) {  
  58.                         throw new RuntimeException("转换json字符失败!");  
  59.                 }  
  60.         }  
  61.           
  62.         public <T> T toObject(String json,Class<T> clazz) {  
  63.                 try {  
  64.                         return mapper.readValue(json, clazz);  
  65.                 } catch (IOException e) {  
  66.                         throw new RuntimeException("将json字符转换为对象时失败!");  
  67.                 }  
  68.         }  
  69.           
  70.         public static class JsonDateSerializer extends JsonSerializer<Date>{  
  71.             private SimpleDateFormat dateFormat;  
  72.             public JsonDateSerializer(String format) {  
  73.                  dateFormat = new SimpleDateFormat(format);  
  74.                 }  
  75.               
  76.             @Override  
  77.             public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)  
  78.                     throws IOException, JsonProcessingException {  
  79.                 String value = dateFormat.format(date);  
  80.                 gen.writeString(value);  
  81.             }  
  82.         }  
  83. }  
再将<mvc:annotation-driven/>改为以下配置,配置一个新的json转换器,将它的ObjectMapper对象设置为JsonUtil中的objectMapper对象,此转换器比spring内置的json转换器优先级更高,所以与json有关的转换,spring会优先使用它。
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <mvc:annotation-driven>  
  2.     <mvc:message-converters>  
  3.         <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
  4.             <property name="objectMapper" value="#{jsonUtil.mapper}"/>  
  5.             <property name="supportedMediaTypes">  
  6.                 <list>  
  7.                     <value>text/json;charset=UTF-8</value>  
  8.                 </list>  
  9.             </property>    
  10.         </bean>  
  11.     </mvc:message-converters>  
  12. </mvc:annotation-driven>  

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

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")  
  2. public Date getCreateTime() {  
  3.     return this.createTime;  
  4. }  
  5. @DateTimeFormat(pattern="yyyy-MM-dd")  
  6. public Date getBirthday() {  
  7.     return this.birthday;  
  8. }  

完毕,一切都完美了。


以下为2014/4/21 补充

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

不说了,直接上方案吧。

1.spring配置照样是这样:

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

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

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.xxx.utils;  
  2.   
  3. import java.io.IOException;  
  4. import java.text.SimpleDateFormat;  
  5. import org.springframework.stereotype.Component;  
  6. import com.fasterxml.jackson.databind.ObjectMapper;  
  7.   
  8. /** 
  9.  * json处理工具类 
  10.  * @author zhangle 
  11.  */  
  12. @Component  
  13. public class JsonUtil {  
  14.   
  15.     private static final String DEFAULT_DATE_FORMAT="yyyy-MM-dd HH:mm:ss";  
  16.     private static final ObjectMapper mapper;  
  17.   
  18.     static {  
  19.         SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);  
  20.         mapper = new ObjectMapper();  
  21.         mapper.setDateFormat(dateFormat);  
  22.     }  
  23.       
  24.     public static String toJson(Object obj) {  
  25.         try {  
  26.             return mapper.writeValueAsString(obj);  
  27.         } catch (Exception e) {  
  28.             throw new RuntimeException("转换json字符失败!");  
  29.         }  
  30.     }  
  31.       
  32.     public <t> T toObject(String json,Class<t> clazz) {  
  33.         try {  
  34.             return mapper.readValue(json, clazz);  
  35.         } catch (IOException e) {  
  36.             throw new RuntimeException("将json字符转换为对象时失败!");  
  37.         }  
  38.     }  
  39. }</t></t>  

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

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")  
  2. @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")  
  3. public Date getCreateTime() {  
  4. return this.createTime;  
  5. }  
  6. @DateTimeFormat(pattern="yyyy-MM-dd")  
  7. @JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")  
  8. public Date getBirthday() {  
  9.     return this.birthday;  
  10. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值