SpringBoot使用@JSONField(format = "yyyy-MM-dd HH:mm:ss")無效的問題

SpringBoot使用@JSONField(format = "yyyy-MM-dd HH:mm:ss")無效的問題vue

實體類中:
@Temporal(TemporalType.TIMESTAMP)
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
@Column(name="V_DATE")
private Date vdate;java

 

1.改成使用JsonFormat(若是JsonFormat也失效,能夠試試下面其餘方法)json

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")orm

 

2.使用JSON規範:
1)導入<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
2)在本身的XXXApplication.java裏添加:
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
  // 1. 須要定義一個converter轉換消息的對象
  FastJsonHttpMessageConverter fasHttpMessageConverter =
  new FastJsonHttpMessageConverter();對象

  // 2. 添加fastjson的配置信息,好比:是否須要格式化返回的json的數據
  FastJsonConfig fastJsonConfig = new FastJsonConfig();
  fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);get

  // 3. 在converter中添加配置信息
  fasHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
  HttpMessageConverter<?> converter = fasHttpMessageConverter;
  //解決中文亂碼
  List<MediaType> fastMediaTypes = new ArrayList<>();
  fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
  fasHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);io

  return new HttpMessageConverters(converter);
}console

或者:ast

@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
  FastJsonHttpMessageConverter fasHttpMessageConverter =
  new FastJsonHttpMessageConverter();
  fasHttpMessageConverter.setDateFormat("yyyy-MM-dd HH:mm:ss");
  return new HttpMessageConverters(fasHttpMessageConverter);function

}


解決了時間問題,可是對個人一些其餘Json格式的內容有影響,前臺可能會獲取不了一些數據

方法:

  將fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

  改成fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,SerializerFeature.DisableCircularReferenceDetect);

3.在vue中使用攔截器:
1)全局使用:
Vue.filter('dateFormat',function(dataStr,pattern=''){
  console.log('調用quan有的');
  var dt = new Date(dataStr);
  var y = dt.getFullYear();
  var m = dt.getMonth()+1;
  var d = dt.getDate();
  if (pattern.toLowerCase() == 'yyyy-mm-dd') {
    return `${y}-${m}-${d}`;
  }else{
    var hh = dt.getHours();
    var mm = dt.getMinutes();
    var ss = dt.getSeconds();
    return `${y}-${m}-${d} ${hh}:${mm}:${ss}` ;
  }
})

2)局部使用:
export default {
  data() {
    ...
  },
  filters:{
    dformat(hirdate){
      var dt = new Date(hirdate);
      var y = dt.getFullYear();
      var m = dt.getMonth()+1;
      var d = dt.getDate();
      var hh = dt.getHours();
      var mm = dt.getMinutes();
      var ss = dt.getSeconds();
      return `${y}-${m}-${d} ${hh}:${mm}:${ss}` ;
    }

  }

} 使用攔截器的調用方式便可。

相關文章
相關標籤/搜索