您的位置:首页 > 编程语言 > Java开发

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");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: