Spring MVC支持Json 【含Jar包】
来源:互联网 发布:音频声音放大软件 编辑:程序博客网 时间:2024/06/04 18:04
一、需要Jar包
1、spring
2、json相关Jar包 主要是jackson-core-lgpl-1.8.5.jar和jackson-mapper-lgpl-1.8.5.jar
下载地址:http://download.csdn.net/detail/ajian11/8289937
二、关键配置
1、web.xml配置解决乱码问题
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2、springmvc-servlet.xml中的关键配置
a、增加3个Bean,实现Spring MVC对Json对象的自动解析和封装
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringHttpMessageConverter" />
<ref bean="byteArrayHttpMessageConverter" />
<ref bean="jsonHttpMessageConverter" />
<!-- <ref bean="jsonHttpMessageConverter4JS" /> -->
</list>
</property>
</bean>
<bean id="byteArrayHttpMessageConverter"
class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean id="stringHttpMessageConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="jsonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json</value>
</list>
</property>
</bean>
b、<!-- mvc注解驱动 -->扫描com.web.controller中的注解
<mvc:annotation-driven />
<context:component-scan base-package="com.web.controller" />
三 com.web.controller包下的jsonHanlder和UserModel实现
@Controller
@RequestMapping("/json")
public class jsonHandler
{
@RequestMapping(value = "/login")
@ResponseBody
public UserModel login(@RequestBody LoginRequestM requestM)
{
UserModel userModel = new UserModel();
userModel.setName("232");
return userModel;
}
}
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) //UserModel被转换为Json结构时,如果其属性中有为空的,则加上该句话后,会被过滤掉,否则都会被转换
@@JsonIgnoreProperties(value = "tel")//该注解作用是忽略对tel属性的Json转换
public class UserModel
{
private String name;
private String tel;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getTel()
{
return tel;
}
public void setTel(String tel)
{
this.tel = tel;
}
}
四、测试html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="./js/test.js"></script>
<script type="text/javascript" src="./js/jquery.min.js"></script>
</head>
<body>
<input type="button" onclick="request();"/>
</body>
</html>
test.js的javascript
function request()
{
var cfg = {
type : 'POST',
data : JSON.stringify({
method : 'upload',
userName:'测试中文乱码',
passWord:'123',
}),
dataType : 'json',
contentType : 'application/json;',
success : function(result) {
alert(1);
}
};
cfg.url = "/JsonTest/json/login";
$.ajax(cfg);
}
- Spring MVC支持Json 【含Jar包】
- Spring MVC jar包详解
- Spring MVC jar包详解
- spring mvc支持返回json
- maven打包含spring的jar包
- spring mvc配置json转换器,jar包冲突导致配置无效
- WebCollector Maven支持(含所有依赖jar包)
- spring boot 支持jar包运行jsp
- spring boot 支持jar包运行jsp
- spring mvc对Json的支持
- Spring MVC 3中各个jar包的作用
- spring mvc的简单搭建+各种jar包的导入
- 创建maven web项目 下载spring MVC jar包
- spring mvc 编译的jar包,部署为windows 服务
- 配置spring mvc碰到的jar包问题
- spring mvc 对返回json格式数据的支持
- spring mvc支持返回json对象,乱码问题
- Spring MVC - 上傳圖片, JSON数据交互 ,RESTful 支持
- c++使用mysql的api连接相关问题
- Effective C++学习笔记(Part Four:Item 18-25)
- Foundation框架常用数据类型和NSAutoreleasePool自动释放池解析
- 第二章结构图
- GetStyle,ModifyStyle,GetExStyle,ModifyStyleEx
- Spring MVC支持Json 【含Jar包】
- QT随机数qsrand();
- 剑指offer 面试题16 反转单链表
- OC语言--Foundation框架
- Effective C++学习笔记(Part Five:Item 26-31)
- Android Studio Tips Of the Day – Roundup #1
- javascript prototype [note]
- 【Python学习笔记】 Python练习题mapreduce
- java操作mongodb(分页优化)