引言
如果我们现在做的项目如果不大,或者对美观要求度不高我们都会使用一些前台的框架比如说easyui,boostrapt。这后台就要给前台提供json数据,在一些app开发中也是如此....
需要使用的jar包
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.9.6</version>
</dependency>
在spring-web.xml里面需要配置
<bean id="jackson2MessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value><!-- 避免IE出现下载JSON文件的情况 -->
</list>
</property>
</bean>
如果我们时间需要格式化 我们需要配置一个
类型转换器
public class CustomObjectMapper extends ObjectMapper {
/**
*
*/
private static final long serialVersionUID = 1L;
public CustomObjectMapper() {
this.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
}
}
然后修改spring-web.xml
<bean id="CustomObjectMapper" class="com.chaojimanong.os.common.Mapper.CustomObjectMapper" />
<bean id="jackson2MessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="CustomObjectMapper"/>
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value><!-- 避免IE出现下载JSON文件的情况 -->
</list>
</property>
</bean>
最后看看效果
时间已经完全格式化,而且我们返回的数据是json
1137

被折叠的 条评论
为什么被折叠?



