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);
 }

0 0
原创粉丝点击