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 楊軍威 閱讀(1186) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 肥西县| 隆回县| 宜宾市| 遵义市| 济宁市| 镇原县| 广汉市| 黑龙江省| 辰溪县| 黔西| 进贤县| 定南县| 和平县| 鹤山市| 日喀则市| 通河县| 昔阳县| 图木舒克市| 建昌县| 普洱| 东乌| 彭州市| 茌平县| 东乡县| 德惠市| 滦南县| 崇州市| 平乡县| 稻城县| 富阳市| 泾阳县| 昆山市| 云龙县| 北票市| 刚察县| 南京市| 大邑县| 边坝县| 资讯 | 博白县| 奉新县|