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

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

Spring REST API 為 pojo 注冊日期轉(zhuǎn)換器

Spring REST API 為 pojo 注冊日期轉(zhuǎn)換器

守著星空守著你 2023-12-13 14:24:43
Spring Rest默認(rèn)提供從路徑變量和url參數(shù)構(gòu)建pojo的功能。就我而言,我有 pojo:public class MyCriteria {  private String from;  private String till;  private Long communityNumber;  private String communityName;}這是我的控制器中使用的。網(wǎng)址是http://localhost:8080/community/{communityNumber}/app. 請求結(jié)果curl "http://localhost:8080/community/1/app?from=2018-11-14&till=2019-05-13&communityName=myCOm"是:{  'from':'2018-11-14';  'till':'2019-05-12';  'communityNumber':'1';  'communityName':'myCOm'}看起來效果很好。在 pojo 數(shù)據(jù)中包含按用途所需的類型會更好。所以我想要有字段from和till類型LocalDate。使用 spring 我希望這個解決方案幾乎是開箱即用的。但由于生命周期,任何彈簧或杰克遜日期轉(zhuǎn)換器都無法解決我的問題。Spring 在注入日期之前驗(yàn)證 pojo 字段的類型,并且我得到類型不匹配異常。我認(rèn)為一般原因是 spring 使用特殊的構(gòu)建器,它嘗試按名稱查找所需的參數(shù),并且忽略要在 pojo 內(nèi)部應(yīng)用的字段的注釋。問題是:是否有任何優(yōu)雅的解決方案可以通過 spring 構(gòu)建 pojo,其中某些字段默認(rèn)會轉(zhuǎn)換為String格式?LocalDate
查看完整描述

4 回答

?
ABOUTYOU

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個贊

這可能對你有幫助。我有類似的情況,我使用這種方法將數(shù)據(jù)轉(zhuǎn)換為特定的需求。


public class MyCriteria {

  public MyCriteria(LocalDate from, LocalDate till, Long communityNumber, String communityName){

   // assignement of variables

}

  private LocalDate from;

  private LocalDate till;

  private Long communityNumber;

  private String communityName;

}

因此,每當(dāng)您從 JSON 創(chuàng)建對象時,它都會根據(jù)要求創(chuàng)建它。


當(dāng)我實(shí)現(xiàn)這個時,我使用“Jackson”的 ObjectMapper 類來完成此操作。希望你也能使用同樣的方法。


查看完整回答
反對 回復(fù) 2023-12-13
?
慕仙森

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個贊

在 pojo 類中使用 java.sql.Date 。就像 private Date from 一樣。我希望它能用于 JSON 轉(zhuǎn)換。當(dāng)您從 UI 接收日期 JSON 字段時,請始終使用 Java.sql.Date 進(jìn)行 Jackson 日期轉(zhuǎn)換。



查看完整回答
反對 回復(fù) 2023-12-13
?
弒天下

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個贊

問題的答案是使用 init binder 來注冊指定條件內(nèi)的類型映射。需要PropertyEditorSupport針對特定目的進(jìn)行實(shí)施。


短代碼示例是:


    @InitBinder

    public void initBinder(WebDataBinder binder) {

        binder.registerCustomEditor(LocalDate.class, new PropertyEditorSupport() {

            @Override

            public void setAsText(String text) throws IllegalArgumentException {

                setValue(LocalDate.parse(text, DateTimeFormatter.ISO_LOCAL_DATE));

            }

        });

    }

完整的代碼示例可以從github獲?。?/p>


import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.InitBinder;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import org.vl.example.rest.dtofromoaramsandpath.web.dto.MyCriteriaLd;


import java.beans.PropertyEditorSupport;

import java.time.LocalDate;

import java.time.format.DateTimeFormatter;


@RestController

@RequestMapping("verify/criteria/mapping")

@Slf4j

public class MyControllerLd {

    @InitBinder

    public void initBinder(WebDataBinder binder) {

        binder.registerCustomEditor(LocalDate.class, new PropertyEditorSupport() {

            @Override

            public void setAsText(String text) throws IllegalArgumentException {

                setValue(LocalDate.parse(text, DateTimeFormatter.ISO_LOCAL_DATE));

            }

        });

    }


    @GetMapping("community/{communityNumber}/dtold")

    public MyCriteriaLd loadDataByDto(MyCriteriaLd criteria) {

        log.info("received criteria: {}", criteria);

        return criteria;

    }

}

因此,這種情況的模型可以是下一個:


import lombok.Data;


import java.time.LocalDate;


@Data

public class MyCriteriaLd {

    private LocalDate from;

    private LocalDate till;

    private Long communityNumber;

    private String communityName;

}


查看完整回答
反對 回復(fù) 2023-12-13
?
30秒到達(dá)戰(zhàn)場

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個贊

為此,您需要添加jsr310依賴項(xiàng)。

compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.8.10")

我希望這對你有用。


查看完整回答
反對 回復(fù) 2023-12-13
  • 4 回答
  • 0 關(guān)注
  • 263 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號