1. 解析json
Fastjson解析json数据,格式化日期
1. 添加依赖包
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency> |
2. 修改MVC配置适配器
创建WebMvcConfigurerAdapter的之类重写消息转换方法
configureMessageConverters:
package com.svw.tbox.tcloud.user.consumer.conf;
import java.util.ArrayList; import java.util.List; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
@Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter {
/** * 利用fastjson替换掉jackson,且解决中文乱码问题 * @param converters */ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); List<MediaType> fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes); fastConverter.setFastJsonConfig(fastJsonConfig); converters.add(fastConverter); }
}
|
3. Model添加日期字段createtime
@JSONField(format="yyyy-MM-dd HH:mm:ss") private Date createTime;
public Date getCreateTime() { returncreateTime; }
publicvoid setCreateTime(Date createTime) { this.createTime = createTime; } |
4. Controller中返回对象添加时间
@RequestMapping("/getUser") public User getUser(){ User u = new User(1L, "小般般", "胡话", 30, new BigDecimal(10000000.00)); u.setCreateTime(new Date()); returnu; } |