springMVC处理json数据 以及ajax乱码如何解决 1/实体类转化成json ======================================================================================================================== 对于封装的bean,返回成json格式,在mvc框架下特别简单,仅仅需要将对象返回,同时在方法上加上注解就可以了,如下: @RequestMapping("testJson") @ResponseBody public User beanToJson(){ User user = new User(11,"张三","123456"); return user; } ________________________________________________________________________________________________________________________ 注意,这里默认的情况下是jackson ,所以需要导入相关的jar文件依赖, 对于习惯使用其他json转换器的人来说可以通过配置 注解驱动力的配置来改装,如阿里的fastJson可以如下:https://www.cnblogs.com/boywwj/p/7441642.html <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json</value> <value>application/xml;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> ________________________________________________________________________________________________________________________ 2/map转化为json @RequestMapping("map") @ResponseBody public Map<String,Object> beanToJson(){ User user = new User(11,"张三","123456"); Map<String,Object> map = new HashMap<>(); map.put("java",123456); map.put("user",user); return map; } ________________________________________________________________________________________________________________________ 3/list转化为json @RequestMapping("list") @ResponseBody public List<Map<String,Object>> listToJson(){ User user = new User(11,"张三","123456"); Map<String,Object> map = new HashMap<>(); map.put("java",123456); map.put("user",user); Map<String,Object> map2 = new HashMap<>(); map2.put("java1",123456); map2.put("user1",user); List<Map<String,Object>> list = new ArrayList<>(); list.add(map); list.add(map2); return list; } ________________________________________________________________________________________________________________________ 4/返回的Boolean和int类型数据如何处理 ======================================================================================================================== @RequestMapping(value = "t1") @ResponseBody public String t1 () { System.out.println("测试返回字符串...."); return "你好..."; } ________________________________________________________________________________________________________________________ 1/ 针对ajax乱码问题的产生,在springmvc中配置这个配置项之后<mvc:annotation-driven>,会默认配置 RequestMappingHandlerAdapter和HttpMessageConverter,当我们使用@ResponseBody时,那么数据返回时会调用这个数据转换器。 经过查看源码可知,默认情况下会转换成ISO-8859-1格式。 进行下面配置可以根治ajax乱码问题: <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/> </bean> </mvc:message-converters> </mvc:annotation-driven> 2/ 针对性乱码解决 @RequestMapping(value = “/test”,produces=”text/html;charset=UTF-8;”) 治标不治本的方式 3/ 对于ajax乱码,在配置过滤器的前提下仍然出现乱码,是因为我们使用了注解,导致字符编码转换器更换,那么如果不使用注解 不就可以了吗 ,事实证明是可以的如下: @RequestMapping("/ajax") public void ajaxCharacter(HttpServletResponse response) throws IOException{ PrintWriter out = response.getWriter(); out.print("测试"); out.close(); } 能解决问题就是王道: 推荐第一种解决方式!!! ========================================================================================================================
09-07
09-07
09-07