又到搭新開(kāi)發(fā)環(huán)境的時(shí)候,總是不免去網(wǎng)上搜下目前最新的框架。spring是web開(kāi)發(fā)必用的框架,于是乎下載了目前最新的spring4.0.3,同時(shí)越來(lái)越不想用struts2,想試試spring mvc,也將spring-webmvc4.0.3下了下來(lái),投入兩天時(shí)間學(xué)習(xí)后,發(fā)現(xiàn)還是挺優(yōu)雅的,特別是從3.0后,spring mvc使用注解方式配制,以及對(duì)rest風(fēng)格的支持,真是完美致極。
下面將這兩天研究到的問(wèn)題做個(gè)總結(jié),供參考。
1.request對(duì)象的獲取
方式1:在controller方法上加入request參數(shù),spring會(huì)自動(dòng)注入,如:
1
|
public String list(HttpServletRequest request,HttpServletResponse response) |
方式2:在controller類(lèi)中加入@Resource private HttpServletRequest request 屬性,spring會(huì)自動(dòng)注入,這樣不知道會(huì)不會(huì)出現(xiàn)線(xiàn)程問(wèn)題,因?yàn)橐粋€(gè)controller實(shí)例會(huì)為多個(gè)請(qǐng)求服務(wù),暫未測(cè)試。
方式3:在controller方法中直接寫(xiě)代碼獲取
1
2
|
HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); |
方式4:在controller中加入以下方法,此方法會(huì)在執(zhí)行此controller的處理方法之前執(zhí)行
1
2
3
4
5
|
@ModelAttribute private void initServlet(HttpServletRequest request,HttpServletResponse response) { //String p=request.getParameter("p"); //this.req=request;//實(shí)例變量,有線(xiàn)程安全問(wèn)題,可以使用ThreadLocal模式保存 } |
2.response對(duì)象的獲取
可以參照以上request的獲取方式1和方式4,方式2和方式3對(duì)response對(duì)象無(wú)效!
3.表單提交之?dāng)?shù)據(jù)填充
直接在方法上加入實(shí)體對(duì)象參數(shù),spring會(huì)自動(dòng)填充對(duì)象中的屬性,對(duì)象屬性名要與<input>的name一致才會(huì)填充,如:
1
|
public boolean doAdd(Demo demo) |
4.表單提交之?dāng)?shù)據(jù)轉(zhuǎn)換-Date類(lèi)型
在實(shí)體類(lèi)的屬性或get方法上加入 @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss"),那么表單中的日期字符串就會(huì)正確的轉(zhuǎn)換為Date類(lèi)型了。還有@NumberFormat注解,暫時(shí)沒(méi)用,就不介紹了,一看就知道是對(duì)數(shù)字轉(zhuǎn)換用的。
5.json數(shù)據(jù)返回
在方法上加入@ResponseBody,同時(shí)方法返回值為實(shí)體對(duì)象,spring會(huì)自動(dòng)將對(duì)象轉(zhuǎn)換為json格式,并返回到客戶(hù)端。如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
|
@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測(cè)試" ); demo.setStatus(( short ) 1 ); return demo; } |
注意:spring配置文件要加上:<mvc:annotation-driven/>,同時(shí)還要引入jackson-core.jar,jackson-databind.jar,jackson-annotations.jar(2.x的包)才會(huì)自動(dòng)轉(zhuǎn)換json
這種方式是spring提供的,我們還可以自定義輸出json,以上第二條不是說(shuō)了獲取response對(duì)象嗎,拿到response對(duì)象后,任由開(kāi)發(fā)人員宰割,想怎么返回就怎么返回。
方法不要有返回值,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@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測(cè)試" ); demo.setStatus(( short ) 1 ); String json=JsonUtil.toJson(obj); //;json處理工具類(lèi) HttpServletResponse response = //獲取response對(duì)象 response.getWriter().print(json); } |
OK,一切很完美。接著惡心的問(wèn)題迎面而來(lái),date類(lèi)型轉(zhuǎn)換為json字符串時(shí),返回的是long time值,如果你想返回“yyyy-MM-dd HH:mm:ss”格式的字符串,又要自定義了。我很奇怪,不是有@DateTimeFormat注解嗎,為什么不利用它。難道@DateTimeFormat只在表單提交時(shí),將字符串轉(zhuǎn)換為date類(lèi)型,而date類(lèi)型轉(zhuǎn)換為json字符串時(shí),就不用了。帶著疑惑查源碼,原來(lái)spring使用jackson轉(zhuǎn)換json字符,而@DateTimeFormat是spring-context包中的類(lèi),jackson如何轉(zhuǎn)換,spring不方便作過(guò)多干涉,于是只能遵守jackson的轉(zhuǎn)換規(guī)則,自定義日期轉(zhuǎn)換器。
先寫(xiě)一個(gè)日期轉(zhuǎn)換器,如下:
1
2
3
4
5
6
7
8
9
|
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); } } |
在實(shí)體類(lèi)的get方法上配置使用轉(zhuǎn)換器,如下:
1
2
3
4
5
|
@DateTimeFormat (pattern= "yyyy-MM-dd HH:mm:ss" ) @JsonSerialize (using=JsonDateSerializer. class ) public Date getCreateTime() { return this .createTime; } |
OK,到此搞定。
你真的滿(mǎn)意了嗎,這么不優(yōu)雅的解決方案,假設(shè)birthday屬性是這樣的,只有年月日,無(wú)時(shí)分秒
1
2
3
4
|
@DateTimeFormat (pattern= "yyyy-MM-dd" ) public Date getBirthday() { return this .birthday; } |
這意味著,又要為它定制一個(gè)JsonDate2Serializer的轉(zhuǎn)換器,然后配置上,像這樣
1
2
3
4
5
|
@DateTimeFormat (pattern= "yyyy-MM-dd" ) @JsonSerialize (using=JsonDate2Serializer. class ) public Date getBirthday() { return this .birthday; } |
假設(shè)還有其它格式的Date字段,還得要為它定制另一個(gè)轉(zhuǎn)換器。my god,請(qǐng)饒恕我的罪過(guò),不要讓我那么難受
經(jīng)過(guò)分析源碼,找到一個(gè)不錯(cuò)的方案,此方案將不再使用@JsonSerialize,而只利用@DateTimeFormat配置日期格式,jackson就可以正確轉(zhuǎn)換,但@DateTimeFormat只能配置在get方法上,這也沒(méi)什么關(guān)系。
先引入以下類(lèi),此類(lèi)對(duì)jackson的ObjectMapper類(lèi)做了注解掃描攔截,使它也能對(duì)加了@DateTimeFormat的get方法應(yīng)用日期格式化規(guī)則
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
|
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處理工具類(lèi) * @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( "轉(zhuǎn)換json字符失敗!" ); } } public <T> T toObject(String json,Class<T> clazz) { try { return mapper.readValue(json, clazz); } catch (IOException e) { throw new RuntimeException( "將json字符轉(zhuǎn)換為對(duì)象時(shí)失敗!" ); } } 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/>改為以下配置,配置一個(gè)新的json轉(zhuǎn)換器,將它的ObjectMapper對(duì)象設(shè)置為JsonUtil中的objectMapper對(duì)象,此轉(zhuǎn)換器比spring內(nèi)置的json轉(zhuǎn)換器優(yōu)先級(jí)更高,所以與json有關(guān)的轉(zhuǎn)換,spring會(huì)優(yōu)先使用它。
1
2
3
4
5
6
7
8
9
10
11
12
|
< 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 > |
接下來(lái)就可以這樣配置實(shí)體類(lèi),jackson也能正確轉(zhuǎn)換Date類(lèi)型
1
2
3
4
5
6
7
8
|
@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; } |
完畢,一切都完美了。
補(bǔ)充
寫(xiě)了那么多,發(fā)現(xiàn)白忙活了一場(chǎng),原來(lái)jackson也有一個(gè)@JsonFormat注解,將它配置到Date類(lèi)型的get方法上后,jackson就會(huì)按照配置的格式轉(zhuǎn)換日期類(lèi)型,而不自定義轉(zhuǎn)換器類(lèi),欲哭無(wú)淚啊。辛苦了那么多,其實(shí)別人早已提供,只是沒(méi)有發(fā)現(xiàn)而已。
不說(shuō)了,直接上方案吧。
1.spring配置照樣是這樣:
1
|
< mvc:annotation-driven > |
2.JsonUtil可以不用了,但如果要自己從response對(duì)象輸出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
|
package com.xxx.utils; import java.io.IOException; import java.text.SimpleDateFormat; import org.springframework.stereotype.Component; import com.fasterxml.jackson.databind.ObjectMapper; /** * json處理工具類(lèi) * @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( "轉(zhuǎn)換json字符失敗!" ); } } public <t> T toObject(String json,Class<t> clazz) { try { return mapper.readValue(json, clazz); } catch (IOException e) { throw new RuntimeException( "將json字符轉(zhuǎn)換為對(duì)象時(shí)失敗!" ); } } } |
3.實(shí)體類(lèi)的get方法就需要多一個(gè)@JsonFormat的注解配置
1
2
3
4
5
6
7
8
9
10
|
@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; } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/zhanngle/article/details/24123659/