springMvc 中返回字符串 乱码解决

/**
	 * produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8"  乱码解决
	 * @param callback
	 * @return
	 */
	@RequestMapping(value="/itemcat/list", produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
	@ResponseBody
	public String getItemCatList(String callback) {
		CatResult itemCatList = itemCatService.getItemCatList();
		String json = JsonUtils.objectToJson(itemCatList);
		String result = callback+"("+json+");";
		return result;
	}

 方法二:返回object,使用工具类自动添加回调函数

@RequestMapping("/itemcat/list2")
	@ResponseBody
	public Object getItemCatList2(String callback) {
		CatResult itemCatList = itemCatService.getItemCatList();
		//使用 MappingJacksonValue 工具类来添加回调函数自动拼装字符串
		MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(itemCatList);
		mappingJacksonValue.setJsonpFunction(callback);
		return mappingJacksonValue;
	}

  

猜你喜欢

转载自www.cnblogs.com/redhat0019/p/10065516.html