spring mvc对Json的支持
2016-08-10 14:58
190 查看
1.设置spring-mvc.xml文件
<?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:p="http://www.springframework.org/schema/p"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 使用注解的包,包括子集 -->
<context:component-scan base-package="com.java1234"/>
<!-- 支持对象与json的转换。 -->
<mvc:annotation-driven/>
<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
2.加jackSon的jar包
3.import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.java1234.model.User;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/ajax")
public @ResponseBody User ajax(){
User user=new User("zhangsan","123");
return user;
}
}
其中的@ResponseBody是将数据转化为json格式
但还是一般都会自己写个util来实现:
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
public class ResponseUtil {
public static void write(HttpServletResponse response,Object o)throws Exception{
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.println(o.toString());
out.flush();
out.close();
}
}
<?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:p="http://www.springframework.org/schema/p"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 使用注解的包,包括子集 -->
<context:component-scan base-package="com.java1234"/>
<!-- 支持对象与json的转换。 -->
<mvc:annotation-driven/>
<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
2.加jackSon的jar包
3.import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.java1234.model.User;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/ajax")
public @ResponseBody User ajax(){
User user=new User("zhangsan","123");
return user;
}
}
其中的@ResponseBody是将数据转化为json格式
但还是一般都会自己写个util来实现:
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
public class ResponseUtil {
public static void write(HttpServletResponse response,Object o)throws Exception{
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.println(o.toString());
out.flush();
out.close();
}
}
相关文章推荐
- fastjson 对spring mvc 支持,直接返回对象时候,出现类名的错误
- Spring MVC - 上傳圖片, JSON数据交互 ,RESTful 支持
- spring mvc支持返回json
- spring mvc超强的json支持,你自己根本不需要额外的配置。spring mvc都给你配置好了!!!
- spring mvc支持返回json对象,乱码问题
- Spring MVC针对IE不支持Content-Type=appliaction/json解决,duang-方案更新了
- Spring MVC--12.对json支持
- Spring MVC支持Json 【含Jar包】
- spring mvc 对返回json格式数据的支持
- Spring MVC之JSON数据交互和RESTful的支持
- Spring MVC关于IE对application/json的content-type不支持解决方案---duang 解决了
- Spring MVC-集成(Integration)-生成JSON示例(转载实践)
- NanShan即时通讯 ie6 ie7 ie8 不支持json 终极解放方案
- Spring MVC 4.1.4 RESTFUL风格返回JSON数据406错误处理
- Spring mvc 使用Gson 返回Json数据 中文乱码问题
- Spring mvc 返回json 字符串的其他方式
- spring mvc整合json
- Spring MVC 中的REST支持
- spring MVC框架下 前台向java后台传送json字符串数据
- Spring MVC返回JSON格式的数据