springboot后端時間到前端,相差8小時,時間格式不對


spring boot后台時間正確,返回給前台的時間不正確,和后台差8個小時

{
    "code": 1,
    "msg": "SUCCESS",
    "result": {
        "extractRecords": null,
        "chargeRecords": [
            {
                "id": 4,
                "account": "1604516",
                "deposit_paid": 500,
                "deposit_paid_time": "2019-05-30T03:01:03.000+0000"
            }
        ]
    }
}

原因是:

spring-boot中對於@RestController或者@Controller+@ResponseBody注解的接口方法的返回值默認是Json格式,

所以當對於date類型的數據,在返回瀏覽器端是會被spring-boot默認的Jackson框架轉換,而Jackson框架默認的時區GMT(相對於中國是少了8小時)。

處理方式:

在application.yml添加配置

spring:
  jackson:
    #日期格式化
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8

再次訪問的數據:

{
    "code": 1,
    "msg": "SUCCESS",
    "result": {
        "extractRecords": null,
        "chargeRecords": [
            {
                "id": 4,
                "account": "1604516",
                "deposit_paid": 500,
                "deposit_paid_time": "2019-05-30 11:01:03"
            }
        ]
    }
}

jackson的全部配置:

spring:
  jackson:
    #日期格式化
    date-format: yyyy-MM-dd HH:mm:ss
    serialization:
       #格式化輸出 
      indent_output: true
      #忽略無法轉換的對象
      fail_on_empty_beans: false
    #設置空如何序列化
    defaultPropertyInclusion: NON_EMPTY
    deserialization:
      #允許對象忽略json中不存在的屬性
      fail_on_unknown_properties: false
    parser:
      #允許出現特殊字符和轉義符
      allow_unquoted_control_chars: true
      #允許出現單引號
      allow_single_quotes: true


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM