一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringMVC @RequestBody Date類型的Json轉換方式

SpringMVC @RequestBody Date類型的Json轉換方式

2022-02-25 13:31低調的JVM Java教程

這篇文章主要介紹了SpringMVC @RequestBody Date類型的Json轉換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

SpringMVC @RequestBody Date類型的Json轉換

正常使用Json或Gson對Date類型序列化成字符串時,得到的是類似”Dec 5, 2017 8:03:34 PM”這種形式的字符串,前端得到了這種格式的很難明白這個具體是什么時間,可讀性很低。

同時如果用這種形式的字符串來反序列化為Date對象,也會失敗,這個過程是不可逆的。如何將Date對象序列化為指定格式的字符串,比如”yyyy-MM-dd”格式的字符串,以Gson的使用為例來說明。

對于Gson對象,可以使用GsonBuilder來實例化

通過GsonBuilder設置DateFormat的格式

?
1
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

經過這樣設置后,使用toJson(Object obj)方法對Date對象序列化時,會輸出”yyyy-MM-dd HH:mm:ss”格式的字符串;

也可以將”yyyy-MM-dd HH:mm:ss”格式的字符串反序列化為一個Date對象。值得注意的是,當一個Date對象未指定”HH:mm:ss”時,會使用當前時間來填充以補齊格式長度。

以上講的是Date對象的序列化和反序列化為字符串的方法,在SpingMVC框架中并不適用,下面講SpringMVC中Date的序列化和反序列化。

SpringMVC中,如果前端以GET的形式傳遞字符串,后端想將此字符串反序列化為Date對象,最常用的就是注冊Formatter對象

以零配置框架為例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class String2DateFormatter implements Formatter<Date> {
    private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String DATE_FORMAT = "yyyy-MM-dd";
    @Override
    public String print(Date object, Locale locale) {
        return new GsonBuilder().setDateFormat(DATE_TIME_FORMAT).create().toJson(object);
    }
    @Override
    public Date parse(String text, Locale locale) throws ParseException {
        if (text.length() > 10) {
            return new SimpleDateFormat(DATE_TIME_FORMAT).parse(text);
        } else {
            return new SimpleDateFormat(DATE_FORMAT).parse(text);
        }
    }
}
public class MvcContextConfig extends WebMvcConfigurerAdapter {
    ......
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(new String2DateFormatter());
    }
    ......
}

當然也可以用配置文件的形式配置,具體方法請百度。

當前端傳遞字符串,Controller用Date類型的參數接受時,會使用Formatter將字符串反序列化為Date對象。

如果前端以POST形式傳遞一個Json對象,對象內部有一個Date屬性,前端傳遞的是字符串,后端用一個標識@RequestBody的復合對象接收時,Formatter是不會起作用的。

此時起作用的是HttpMessageConverter的實現類。正常情況下項目內有Jackson或Gson依賴,能夠將Json反序列化為復合對象。

如果依賴了Jackson,且使用Jackson的HttpMessageConverter反序列化Json,那么僅支持反序列化簡單數據類型的屬性,不支持Date類型;但是如果是Gson類型,是支持”yyyy-MM-dd HH:mm:ss”格式的反序列化的,確定不支持”yyyy-MM-dd”格式,其他格式不確定。

也就是說依賴Gson可以將前端的”yyyy-MM-dd HH:mm:ss”格式的字符串反序列化為Date對象,但是將Date對象返回給前端時,解析得到的還是類似”Dec 5, 2017 8:03:34 PM”這種形式的字符串,并不可取。

當我們使用Jackson作為Json對象的序列化和反序列化的解析器時

以零配置形式框架下的代碼實現為例講解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class MvcContextConfig extends WebMvcConfigurerAdapter {
    ......
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        stringConverter.setWriteAcceptCharset(false);
        converters.add(stringConverter);
        converters.add(new ByteArrayHttpMessageConverter());
        converters.add(new ResourceHttpMessageConverter());
        converters.add(new MappingJackson2XmlHttpMessageConverter());
        //設置Date類型使用HttpMessageConverter轉換后的格式,或者注冊一個GsonHttpMessageConverter,能直接支持字符串到日期的轉換
        //當指定了日期字符串格式后,如果傳的日志格式不符合,則會解析錯誤
        converters.add(new MappingJackson2HttpMessageConverter(
            new ObjectMapper().setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))));
        //GsonHttpMessageConverter不支持yyyy-MM-dd形式的字符串轉換為日期
        //converters.add(new GsonHttpMessageConverter());
    }
    ......
}

當我們選擇使用Jackson作為Json的解析器時,需要注冊一個MappingJackson2HttpMessageConverter,對內部默認的objectMapper對象做一個拓展,需要指定日期格式化器,當我們指定了具體的格式時,只支持這種格式的轉換,其他的格式轉換時會報錯。

因此需要前端在傳遞日期字符串時,加上默認的時間,比如”2017-12-2 00:00:00”,雖然多了點工作,但是能確保格式轉換的正確。

當然并不是一定要”yyyy-MM-dd HH:mm:ss”,其他的格式也都支持的,比如”yyyy-MM-dd”等等,具體可以看項目需求自定義,前端傳遞日期字符串的格式需要符合自定義的格式。

當配置了DateFormat時,傳遞對象給前端,對象內部有Date屬性,也會將其序列化為這個格式的字符串。

XML文件形式配置HttpMessageConverter的方法可自行百度。

@RequestBody接收json字符串,自動將日期字符串轉換為java.util.Date

1.配置springMVC可以接收json字符串

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.1.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
 <!-- 解決@ResponseBody返回中文亂碼,解決@RequestBody接收Json字符串自動轉換為實體、List、Map格式轉換器 -->
 <bean
  class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
  <property name="messageConverters">
   <list>
    <!--
    <bean
     class="org.springframework.http.converter.StringHttpMessageConverter">
     <property name="supportedMediaTypes">
      <list>
       <value>text/html;charset=UTF-8</value>
      </list>
     </property>
    </bean>
    -->
    <bean
     class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
     <property name="supportedMediaTypes">
      <list>
       <value>application/json;charset=UTF-8</value>
      </list>
     </property>
    </bean>
   </list>
  </property>
 </bean>
 <!-- 掃描包,應用Spring的注解 -->
 <context:component-scan base-package="com.mvc.action"></context:component-scan>
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  p:viewClass="org.springframework.web.servlet.view.JstlView"
  p:prefix="/"
  p:suffix=".jsp">
 </bean>
 <!-- SpringMVC自定義攔截器,使SpringMVC開啟CORS支持 -->
 <!--
 <mvc:interceptors>
  <mvc:interceptor>
   <mvc:mapping path="/*"/>
   <bean class="com.mvc.dao.CorsInterceptor"></bean>
  </mvc:interceptor>
 </mvc:interceptors>
 -->
 <context:annotation-config/>
 <mvc:annotation-driven/>
</beans>

2.@Controller類代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@RequestMapping(value="/addDicAppUsers.do")
 @ResponseBody
 public boolean addDicAppUsers(@RequestBody DicAppUsersModel dicAppUsersModel)
 {
  if(dicAppUsersService.addDicAppUsers(dicAppUsersModel))
  {
   return true;
  }
  else
  {
   return false;
  }
 }

3.實體類對象代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.mvc.model;
import java.util.Date;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import com.mvc.imp.DateJsonDeserializer;
import com.mvc.imp.DateJsonSerializer;
 
/**
 * 用戶視圖類
 * @author suyunlong
 *
 */
@SuppressWarnings("serial")
public class DicAppUsersModel implements java.io.Serializable
{
 private long id;
 private String loginid;
 private String loginname;
 private String loginpassword;
 private String loginunitcode;
 private String workplace;
 @JsonSerialize(using=DateJsonSerializer.class)
 @JsonDeserialize(using=DateJsonDeserializer.class)
 private Date addtime;
 private long sourceid;
 @JsonSerialize(using=DateJsonSerializer.class)
 @JsonDeserialize(using=DateJsonDeserializer.class)
 private Date createdate;
 public DicAppUsersModel() {
  super();
 }
 public DicAppUsersModel(long id, String loginid, String loginname,
   String loginpassword, String loginunitcode, String workplace,
   Date addtime, long sourceid, Date createdate) {
  super();
  this.id = id;
  this.loginid = loginid;
  this.loginname = loginname;
  this.loginpassword = loginpassword;
  this.loginunitcode = loginunitcode;
  this.workplace = workplace;
  this.addtime = addtime;
  this.sourceid = sourceid;
  this.createdate = createdate;
 }
 public long getId() {
  return id;
 }
 public void setId(long id) {
  this.id = id;
 }
 public String getLoginid() {
  return loginid;
 }
 public void setLoginid(String loginid) {
  this.loginid = loginid;
 }
 public String getLoginname() {
  return loginname;
 }
 public void setLoginname(String loginname) {
  this.loginname = loginname;
 }
 public String getLoginpassword() {
  return loginpassword;
 }
 public void setLoginpassword(String loginpassword) {
  this.loginpassword = loginpassword;
 }
 public String getLoginunitcode() {
  return loginunitcode;
 }
 public void setLoginunitcode(String loginunitcode) {
  this.loginunitcode = loginunitcode;
 }
 public String getWorkplace() {
  return workplace;
 }
 public void setWorkplace(String workplace) {
  this.workplace = workplace;
 }
 public Date getAddtime() {
  return addtime;
 }
 public void setAddtime(Date addtime) {
  this.addtime = addtime;
 }
 public long getSourceid() {
  return sourceid;
 }
 public void setSourceid(long sourceid) {
  this.sourceid = sourceid;
 }
 public Date getCreatedate() {
  return createdate;
 }
 public void setCreatedate(Date createdate) {
  this.createdate = createdate;
 }
}

4.DateJsonSerializer類代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.mvc.imp;
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;
 
public class DateJsonSerializer extends JsonSerializer<Date>
{
 public static final SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 public void serialize(Date date,JsonGenerator jsonGenerator,SerializerProvider serializerProvider)
   throws IOException,JsonProcessingException
 {
  jsonGenerator.writeString(format.format(date)); 
    
}

5.DateJsonDeserializer類代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.mvc.imp;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;
 
public class DateJsonDeserializer extends JsonDeserializer<Date>
{
 public static final SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 public Date deserialize(JsonParser jsonParser,DeserializationContext deserializationContext)
   throws IOException,JsonProcessingException
 {
  try
  {
   return format.parse(jsonParser.getText());
  }
  catch(Exception e)
  {
   System.out.println(e.getMessage());
   throw new RuntimeException(e);
  }
 }
}

這樣,就可以把接收到的json日期字符串轉換為Date了。后面,就可以直接通過Date類型保存日期數據了。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://jiangjibo.blog.csdn.net/article/details/78724737

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久精品国产免费播高清无卡 | 羞羞视频污| 欧美日韩国产一区二区三区在线观看 | 99热国产这里只有精品99 | 西西人体大胆啪啪私拍色约约 | 色综合天天网 | www.国产在线观看 | 美女翘臀内疯狂进出 | 青春草视频在线免费观看 | 久草在线福利视频在线播放 | 短篇小说肉 | 日本三级免费网站 | 久热这里在线精品 | tobu8中国在线观看免费视频 | 91精品国产91久久久久久 | 国产成人www免费人成看片 | ai换脸明星造梦工厂忘忧草 | 午夜精品久久久内射近拍高清 | 精品久久久久亚洲 | 99精品国产高清一区二区三区香蕉 | 国产精品毛片高清在线完整版 | 98精品全国免费观看视频 | 亚洲精品视频一区 | 欧美在线视频一区 | 波多野结衣教师未删减版 | 日韩毛片基地一区二区三区 | 欧美成a人片免费看久久 | 男人吃奶动态图 | 国产麻豆剧果冻传媒观看免费视频 | 欧美在线播放一区二区 | 福利久草| 紧身裙女教师波多野结衣 | 国产香蕉国产精品偷在线观看 | 星空无限传媒xk8046 | 亚洲va韩国va欧美va天堂 | 欧美同性猛男videos | 无码任你躁久久久久久久 | 黑人巨鞭大战白妞10级 | 国产成人高清视频 | 91进入蜜桃臀在线播放 | 免费在线观看网址入口 |