第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

確保 GET/POST 請求上的 ObjectMapper 行為一致

確保 GET/POST 請求上的 ObjectMapper 行為一致

Smart貓小萌 2023-09-06 14:53:09
我有一個類似于以下的方法,該方法已經存在并且工作正常:@PostMapping(      value = "/store",      consumes = "application/json")public ResponseEntity<String> postConversionEvent(@RequestBody Event event) {  ...}現(xiàn)在還需要通過 GET 方法發(fā)布信息,如下所示:@GetMapping("/store")public ResponseEntity<String> postConversionEventAsGet(Event event) {  ...}出于所有意圖和目的,我們可以假設該類Event看起來包含單個org.joda.time.DateTime字段。為了支持這一點,我有一個自定義ObjectMapper支持從字符串時間戳解析DateTimes 。例如以下請求可以正常工作:POST 正文至/store:{  "date": "238572349834"}但是,當我將其作為 GET 發(fā)送時,例如:https://someUrl.com/store?date=238572349834我收到以下錯誤:Field error in object 'event' on field 'date': rejected value [238572349834]; codes [typeMismatch.Event.date,typeMismatch.date,typeMismatch.org.joda.time.DateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [Event.date,date]; arguments []; default message [date]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.joda.time.DateTime' for property 'date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [org.joda.time.DateTime] for value '238572349834'; nested exception is java.lang.IllegalArgumentException: Invalid format: "238572349834" is malformed at "8572349834"]看起來這個解析沒有使用ObjectMapper與 POST 方法相同的方法。有什么辦法可以解決這個問題嗎?
查看完整描述

1 回答

?
慕田峪9158850

TA貢獻1794條經驗 獲得超7個贊

看起來 GET 請求不是使用 序列化的ObjectMapper,而是使用 映射的WebDataBinder。


這個類被創(chuàng)建:


public class DateTimeFromTimestampEditor extends PropertyEditorSupport {


  @Override

  public String getAsText() {

    return Long.toString(((DateTime) getValue()).getMillis());

  }


  @Override

  public void setAsText(String text) throws IllegalArgumentException {

    setValue(new DateTime(Long.parseLong(text), DateTimeZone.UTC));

  }


}

然后我將此方法添加到控制器中:


@InitBinder

public void dataBinding(WebDataBinder binder) {

  binder.registerCustomEditor(DateTime.class, new DateTimeFromTimestampEditor());

}

之前失敗的 GET 請求 ( https://someUrl.com/store?date=238572349834) 現(xiàn)在可以成功反序列化。


查看完整回答
反對 回復 2023-09-06
  • 1 回答
  • 0 關注
  • 97 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號