SpringMVC接收JSON数据
Spring mvc处理json需要使用jackson的类库,因此为支持json格式的输入输出需要先修改pom.xml增加jackson包的引用
<!-- 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格式的数据输入。
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); }
1:使用@RequestBody来设置输入
@RequestMapping("/json1")
@ResponseBody
public JsonResult testJson1(@RequestBody User user){
log.info("get json input from request body annotation");
log.info(user.getUserName());
return new JsonResult(true,"return ok");
}
2:使用HttpEntity来实现输入绑定
@RequestMapping("/json2")
public ResponseEntity<JsonResult> testJson2(HttpEntity<User> user){
log.info("get json input from HttpEntity annotation");
log.info(user.getBody().getUserName());
ResponseEntity<JsonResult> responseResult = new ResponseEntity<JsonResult>( new JsonResult(true,"return ok"),HttpStatus.OK);
return responseResult;
}
SpringMVC返回JSON数据
1:使用@ResponseBody来设置输出内容为context body
2:返回值设置为ResponseEntity<?>类型,以返回context body
3:使用ContentNegotiatingViewResolver来设置输出为json格式,需要修改servlet context配置文件如下
<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类名:{内容}} 的json格式, 例如,以下代码
@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}的格式来返回数据的。 修改方法,重载MappingJacksonJsonView类并重写filterModel方法如下
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; } }
对应的ContentNegotiatingViewResolver修改如下
<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>
本文介绍了如何在SpringMVC中接收和返回JSON数据。首先,为了处理JSON,需要在pom.xml中引入Jackson库。接收JSON数据时,可以使用@RequestBody注解和HttpEntity。返回JSON数据时,通过@ResponseBody指定输出内容,并使用ResponseEntity和ContentNegotiatingViewResolver进行配置。然而,默认情况下,返回的JSON格式会包含模型类名,要避免这种情况,需要自定义MappingJacksonJsonView并重写filterModel方法。

239

被折叠的 条评论
为什么被折叠?



