日期:2014-05-16 浏览次数:20402 次
<!-- 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>
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); }
@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"); }
@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; }
<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>
@RequestMapping("/json3.json") public JsonResult testJson3(@RequestBody User u){ log.info("handle json output from ContentNegotiatingViewResolver"); return new JsonResult(true,"return ok"); }
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; } }
<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>