@JsonFormat

  • 物件<->JSON 格式化互轉
  • 格式前端到後端、格式後端到前端
  • 資料交換如果以JSON交換,用此就可以了

@DateTimeFormat

  • spring mvc 格式前端到後端資料
  • 適用在form mapping

 

JSON傳值

{"testTimeA": "2020/08/26 18:42:12"}

後台設定物件

@lombok.Data
@ApiModel
public class testReq {

  @ApiModelProperty
  @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", shape = JsonFormat.Shape.STRING, timezone = "GMT+8")
  private Date testTime;
}

==> 
back-end value:  testTime=Wed Aug 26 18:42:12 CST 2020
retuen to front-end json= { "testTime": "2020/08/26 18:42:12" }


@lombok.Data
@ApiModel
public class testReq {

  @ApiModelProperty
  private Date testTime;  //如果是特殊格式,會轉換失敗,最好還是自訂pattern 避免後續開發困擾
}

==> 
EXCEPTION: 

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String \"2020/08/26 18:42:12\": not a valid representation (error: Failed to parse Date value '2020/08/26 18:42:12': Cannot parse date \"2020/08/26 18:42:12\": not compatible with any of standard forms (\"yyyy-MM-dd'T'HH:mm:ss.SSSZ\", \"yyyy-MM-dd'T'HH:mm:ss.SSS\", \"EEE, dd MMM yyyy HH:mm:ss zzz\", \"yyyy-MM-dd\"));

arrow
arrow

    咪卡恰比 發表在 痞客邦 留言(0) 人氣()