当前位置: 移动技术网 > IT编程>开发语言>Java > 详解spring mvc4使用及json 日期转换解决方案

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

又到搭新开发环境的时候,总是不免去网上搜下目前最新的框架。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配置文件要加上:<mvc:annotation-driven/>,同时还要引入jackson-core.jar,jackson-databind.jar,jackson-annotations.jar(2.x的包)才会自动转换json

这种方式是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); 
} 

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的转换规则,自定义日期转换器。

先写一个日期转换器,如下:

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); 
 } 
} 

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

@datetimeformat(pattern="yyyy-mm-dd hh:mm:ss") 
@jsonserialize(using=jsondateserializer.class) 
public date getcreatetime() { 
 return this.createtime; 
} 

ok,到此搞定。

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

@datetimeformat(pattern="yyyy-mm-dd") 
public date getbirthday() { 
 return this.birthday; 
} 

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

@datetimeformat(pattern="yyyy-mm-dd") 
@jsonserialize(using=jsondate2serializer.class) 
public date getbirthday() { 
 return this.birthday; 
} 

假设还有其它格式的date字段,还得要为它定制另一个转换器。my god,请饶恕我的罪过,不要让我那么难受

经过分析源码,找到一个不错的方案,此方案将不再使用@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/>改为以下配置,配置一个新的json转换器,将它的objectmapper对象设置为jsonutil中的objectmapper对象,此转换器比spring内置的json转换器优先级更高,所以与json有关的转换,spring会优先使用它。

<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; 
} 

完毕,一切都完美了。

 补充

写了那么多,发现白忙活了一场,原来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字符转换为对象时失败!"); 
  } 
 } 
} 

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; 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网