springMVC学习笔记(七) ---- json
2016-12-06 20:27
495 查看
xml: 使用fastjson的配置,具体配置可以参考fastjson在github上的官方配置:https://github.com/alibaba/fastjson/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--结果视图的前缀+后缀--> <property name="prefix" value="/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <context:component-scan base-package="com.controller"/> <!--json配置--> <bean id="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig"> <!-- Default charset --> <property name="charset" value="UTF-8"/> <!-- Default dateFormat --> <property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"/> <!-- Feature --> <property name="features"> <list> <value>AllowArbitraryCommas</value> <value>AllowUnQuotedFieldNames</value> <value>DisableCircularReferenceDetect</value> </list> </property> </bean> <mvc:annotation-driven> <mvc:message-converters> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <!-- MediaTypes --> <property name="supportedMediaTypes"> <list> <value>application/json</value> </list> </property> <!-- FastJsonConfig --> <property name="fastJsonConfig" ref="fastJsonConfig"/> </bean> </mvc:message-converters> </mvc:annotation-driven> <mvc:default-servlet-handler/>
package com.controller; import com.alibaba.fastjson.JSON; import com.vo.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by cfxd000 on 2016/11/17. */ @Controller @RequestMapping("/") public class JsonController { @ResponseBody @RequestMapping("/json1") public List<User> json1(){ List<User> temp = new ArrayList<User>(); temp.add(new User(1,"json1",111)); temp.add(new User(2,"json1",222)); return temp; } @ResponseBody @RequestMapping("/json2") public Map json2(){ Map temp = new HashMap(); temp.put("u1",new User(3,"json2",333)); temp.put("u2",true); temp.put("u3",123); temp.put("u4","nima"); return temp; } @ResponseBody @RequestMapping("json3") public String json3(){ String jsonStr = JSON.toJSONString(new User(3,"aha",23)); System.out.println(jsonStr); return jsonStr; } @ResponseBody @RequestMapping("json4") public void json4(HttpServletRequest request, HttpServletResponse response) throws IOException { response.getWriter().print("caonima"); } }
相关文章推荐
- 彻底搞懂HashMap,HashTable,ConcurrentHashMap之关联.
- springMVC学习笔记(六) ---- 文件上传
- Java根据身份证号得到年龄通用方法
- springMVC学习笔记(五) ---- 数据处理的几种方式
- Java代码实现一维数组的二分查找(折半查找)(非递归方法)
- java中使用正则验证的写法
- 简单使用SAXReader解析xml数据
- LeetCode 409. Longest Palindrome(Java)
- Spring Boot 菜鸟教程 17 Cache-缓存
- JAVA WEB 浏览器兼容问题汇总
- Java并发编程(二): Java并发机制的底层实现原理
- JVM8-Class文件格式解读及其修改
- Eclipse-JDT的使用
- JAVA中int、String的类型转换
- Java复习总结——框架篇(二)
- Eclipse 导出为可执行JAR
- Java编程军规
- Java 编程规范
- java深入理解---异步回调机制实例解析
- Struts 的简单理解