java学习

java学习

 

在springboot中fastjson的配置

第一种配置方法:
在实体类中加入格式化属性的注解
public class User implements Serializable{
/**
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String username;
private Date birthday;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@JSONField(format="yyyy-MM-dd hh:MM:ss")
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
2.在启动类中继承类
@SpringBootApplication
public class Demo1Application2 extends WebMvcConfigurerAdapter{
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
FastJsonHttpMessageConverter fastConverter=new  FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
public static void main(String[] args) {
SpringApplication app=new SpringApplication(Demo1Application2.class);
ConfigurableApplicationContext context = app.run( args);
//context.close();
}
}
3.访问页面,请求方法,得到结果
{ "age":11, "birthday":"2018-03-15 10:03:55", "id":1, "username":"dddd" }
第二种配置方法:
在启动类加入一个bean
@SpringBootApplication
public class Demo1Application3 {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter=new  FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converters=fastConverter;
return new HttpMessageConverters(converters);
}
public static void main(String[] args) {
SpringApplication app=new SpringApplication(Demo1Application3.class);
ConfigurableApplicationContext context = app.run( args);
//context.close();
}
}

posted on 2018-03-15 10:36 杨军威 阅读(1160) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜