spring中MappingJacksonHttpMessageConverter的使用

	// 需要在web.xml多配置一个拦截
	@RequestMapping("selectOne.json")
	@ResponseBody
	public HashMap selectOnejson(String sqlId, HttpServletRequest request) {
		@SuppressWarnings("unchecked")
		HashMap result = (HashMap) MybatisUtils.selectOne(sqlId, SpringUtils.getParameter(request));
		return result;
	}
	
	// .html后缀的不能使用spring自带的MappingJacksonHttpMessageConverter
	@RequestMapping("selectOne.html")
	@ResponseBody
	public String selectOne(String sqlId, HttpServletRequest request) throws JsonGenerationException, JsonMappingException, IOException {
		@SuppressWarnings("unchecked")
		HashMap result = (HashMap) MybatisUtils.selectOne(sqlId, SpringUtils.getParameter(request));
		ObjectMapper o = new ObjectMapper();
		return o.writeValueAsString(result);
	}
	
	  
		
			
		
	  
	

	
		springServlet
		*.html
	
	
		springServlet
		*.json
	


我觉得spring mvc中的url这样处理不太好,也许可以指定某个属性,让它不根据url判断返回的内容是格式,虽然ajax提交时已经指定为json,dataType:"json",但不起作用。最后还是情愿手工转成String返回来。

MVC通过Ajax获取JSON数据报406错误

你可能感兴趣的:(json)