springmvc ajax jackson

如下处理的好处为:

1.不用因为查询而另外定义bean(新增,修改:做bean的映射比较好)

2.415的处理为修改ajaxcontentType:'applition/json',默认传递方式会报不支持

$.ajax({
				url:'...',
				data: {data:JSON.stringify(param)},
				dataType: 'json',
				success : function(results){
					if(results.result) {
						alert('totalSize : '+ results.totalSize);
//						_reFreshTable(results);
					}
				}
			})
@RequestMapping(value="/query")
	public @ResponseBody ObjectNode queryInfo(HttpServletRequest request) {
		
		String data = request.getParameter("data");
		
		ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
		JsonNode jsonNode = null;
		try {
			jsonNode = mapper.readValue(data, JsonNode.class);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//do some query
		//deal the result
		
		JsonNode node = jsonNode;
		if (node != null) {
			System.out.println("extCustOrderId : "+node.path("extCustOrderId").getTextValue());
		}
		
//		Map<String, Object> returnMap = new HashMap<String, Object>();  
		ObjectNode rootObj = mapper.createObjectNode();
		ObjectNode termInfoBackList = mapper.createObjectNode();
		
		termInfoBackList.put("test", "good");
		
		rootObj.put("result", "true");  
		rootObj.put("totalSize", "22");  
		rootObj.put("termInfoBackList", termInfoBackList);  
		
		return rootObj; 
	}

相关推荐