Spring mvc處理json需要使用jackson的類庫,因此為支持json格式的輸入輸出需要先修改pom.xml增加jackson包的引用
1
2
3
4
5
6
7
8
9
10
11
12
|
<!-- json --> < dependency > < groupId >org.codehaus.jackson</ groupId > < artifactId >jackson-core-lgpl</ artifactId > < version >1.8.1</ version > </ dependency > < dependency > < groupId >org.codehaus.jackson</ groupId > < artifactId >jackson-mapper-lgpl</ artifactId > < version >1.8.1</ version > </ dependency > |
先修改之前的helloworld.jsp,增加客戶端json格式的數(shù)據(jù)輸入。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
var cfg = { type: 'POST' , data: JSON.stringify({userName: 'winzip' ,password: 'password' ,mobileNO: '13818881888' }), dataType: 'json' , contentType: 'application/json;charset=UTF-8' , success: function (result) { alert(result.success); } }; function doTestJson(actionName){ cfg.url = actionName; $.ajax(cfg); } |
根據(jù)前面的分析,在spring mvc中解析輸入為json格式的數(shù)據(jù)有兩種方式 1:使用@RequestBody來設(shè)置輸入
1
2
3
4
5
6
7
|
@RequestMapping ( "/json1" ) @ResponseBody public JsonResult testJson1( @RequestBody User u){ log.info( "get json input from request body annotation" ); log.info(u.getUserName()); return new JsonResult( true , "return ok" ); } |
2:使用HttpEntity來實現(xiàn)輸入綁定
1
2
3
4
5
6
7
|
@RequestMapping ( "/json2" ) public ResponseEntity<JsonResult> testJson2(HttpEntity<User> u){ log.info( "get json input from HttpEntity annotation" ); log.info(u.getBody().getUserName()); ResponseEntity<JsonResult> responseResult = new ResponseEntity<JsonResult>( new JsonResult( true , "return ok" ),HttpStatus.OK); return responseResult; } |
Json格式的輸出也對應(yīng)有兩種方式 1:使用@responseBody來設(shè)置輸出內(nèi)容為context body 2:返回值設(shè)置為ResponseEntity<?>類型,以返回context body 另外,第三種方式是使用ContentNegotiatingViewResolver來設(shè)置輸出為json格式,需要修改servlet context配置文件如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< bean class = "org.springframework.web.servlet.view.ContentNegotiatingViewResolver" > < property name = "order" value = "1" /> < property name = "mediaTypes" > < map > < entry key = "json" value = "application/json" /> </ map > </ property > < property name = "defaultViews" > < list > < bean class = "org.springframework.web.servlet.view.json.MappingJacksonJsonView" /> </ list > </ property > < property name = "ignoreAcceptHeader" value = "true" /> </ bean > |
但這種格式的輸出會返回{model類名:{內(nèi)容}} 的json格式, 例如,以下代碼
1
2
3
4
5
|
@RequestMapping ( "/json3.json" ) public JsonResult testJson3( @RequestBody User u){ log.info( "handle json output from ContentNegotiatingViewResolver" ); return new JsonResult( true , "return ok" ); } |
期望的返回是 {success:true,message:”return ok”}; 但實際返回的卻是 {"jsonResult":{"success":true,"msg":"return ok"}} 原因是MappingJacksonJsonView中對返回值的處理未考慮modelMap中只有一個值的情況,直接是按照mapName:{mapResult}的格式來返回數(shù)據(jù)的。 修改方法,重載MappingJacksonJsonView類并重寫filterModel方法如下
1
2
3
4
5
6
7
8
|
protected Object filterModel(Map<String, Object> model) { Map<?, ?> result = (Map<?, ?>) super .filterModel(model); if (result.size() == 1 ) { return result.values().iterator().next(); } else { return result; } } |
對應(yīng)的ContentNegotiatingViewResolver修改如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< bean class = "org.springframework.web.servlet.view.ContentNegotiatingViewResolver" > < property name = "order" value = "1" /> < property name = "mediaTypes" > < map > < entry key = "json" value = "application/json" /> </ map > </ property > < property name = "defaultViews" > < list > < bean class = "net.zhepu.json.MappingJacksonJsonView" /> </ list > </ property > < property name = "ignoreAcceptHeader" value = "true" /> </ bean > |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/crazy-fox/archive/2012/02/18/2357688.html