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)  編輯  收藏


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


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 平和县| 米泉市| 临安市| 崇信县| 屯留县| 喀喇沁旗| 静乐县| 大荔县| 鲜城| 章丘市| 兰州市| 临江市| 望城县| 丰宁| 若羌县| 宣武区| 佛学| 金堂县| 安福县| 峡江县| 星子县| 孟连| 甘谷县| 思南县| 襄城县| 丰原市| 富宁县| 龙井市| 治多县| 醴陵市| 乌恰县| 衡阳市| 米林县| 康定县| 鄂托克前旗| 通辽市| 建宁县| 驻马店市| 庄浪县| 华池县| 改则县|