最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringMVC之@InitBinder注解詳解

 更新時間:2024年01月19日 10:10:19   作者:CUIYD_1989  
這篇文章主要介紹了SpringMVC之@InitBinder注解詳解,springmvc并不是能對所有類型的參數(shù)進(jìn)行綁定的,如果對日期Date類型參數(shù)進(jìn)行綁定,就會報錯IllegalStateException錯誤,需要的朋友可以參考下

@InitBinder注解的作用

springmvc并不是能對所有類型的參數(shù)進(jìn)行綁定的,如果對日期Date類型參數(shù)進(jìn)行綁定,就會報錯IllegalStateException錯誤。

所以需要注冊一些類型綁定器用于對參數(shù)進(jìn)行綁定。InitBinder注解就有這個作用。

程序代碼示例:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
@RequestMapping("/date")
public class InitBinderController {
    @RequestMapping(value = "/testInitBinder", method = RequestMethod.GET)
    private String testInitBinder(Date date) {
        System.out.println("date = " + date);
        return "RequsetInitBindDemo";
    }
}

postman測試:

在這里插入圖片描述

不能把String類型轉(zhuǎn)換為Date類型報錯。

此時就需要一個日期類型轉(zhuǎn)換器。

import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
@RequestMapping("/date")
public class InitBinderController {
    @RequestMapping(value = "/testInitBinder", method = RequestMethod.GET)
    private String testInitBinder(Date date) {
        System.out.println("date = " + date);
        return "RequsetInitBindDemo";
    }
    @InitBinder
    public void dateTypeBinder(WebDataBinder webDataBinder) {
        //往數(shù)據(jù)綁定器中添加一個DateFormatter日期轉(zhuǎn)化器。
        webDataBinder.addCustomFormatter(new DateFormatter("yyyy-mm-dd"));
    }
}

postman測試:

在這里插入圖片描述

打印結(jié)果:

date = Tue Jan 15 00:05:00 CST 2019

InitBinder注解源碼

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface InitBinder {

	//指定參數(shù)名,這個不知控制器方法上形參的參數(shù)名,而是請求參數(shù)名,
	//可以指定多個。指定后只有這些參數(shù)需要用到該轉(zhuǎn)換器。如果不指定,默認(rèn)所有。
	String[] value() default {};

}

注意:并且使用InitBinder 注冊的綁定器只有在當(dāng)前Controller中才有效,不會作用于其他Controller。

此時,就需要用到@ControllerAdvice注解定義全局綁定器。使不同controller的方法都能作用到。

import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
@ControllerAdvice
public class InitConfig {
    @InitBinder
    public void dateTypeBinder(WebDataBinder webDataBinder) {
        //往數(shù)據(jù)綁定器中添加一個DateFormatter日期轉(zhuǎn)化器。
        webDataBinder.addCustomFormatter(new DateFormatter("yyyy-mm-dd"));
    }
}

使用其他格式轉(zhuǎn)化器

我們可以自定義格式轉(zhuǎn)化器,實現(xiàn)Formatter接口就可。還可以添加驗證器等等。

public class StringFormatter implements Formatter<String> {
    private static final String PREFIX = "convertString == ";

    @Override
    public String parse(String text, Locale locale) throws ParseException {
    	//所以String類型參數(shù)都加上一個前綴。
        String result = PREFIX + text;
        return result;
    }

    @Override
    public String print(String object, Locale locale) {
        return object;
    }
}

添加:

import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

@ControllerAdvice
public class InitConfig {

    @InitBinder
    public void dateTypeBinder(WebDataBinder webDataBinder) {
        //往數(shù)據(jù)綁定器中添加一個DateFormatter日期轉(zhuǎn)化器。
        webDataBinder.addCustomFormatter(new DateFormatter("yyyy-mm-dd"));

        //添加一個string類型的數(shù)據(jù)綁定器,作用是加個前綴
        webDataBinder.addCustomFormatter(new StringFormatter());

    }
}

測試:

@RequestMapping(value = "/testInitBinder2", method = RequestMethod.GET)
    private String testInitBinder2(String name) {
        System.out.println("name = " + name);
        return "RequsetInitBindDemo";
    }

在這里插入圖片描述

打印結(jié)果:

name = convertString == 劉亦菲

到此這篇關(guān)于SpringMVC之@InitBinder注解詳解的文章就介紹到這了,更多相關(guān)@InitBinder注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot項目實現(xiàn)短信發(fā)送接口開發(fā)的實踐

    SpringBoot項目實現(xiàn)短信發(fā)送接口開發(fā)的實踐

    本文主要介紹了SpringBoot項目實現(xiàn)短信發(fā)送接口開發(fā)的實踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • SpringBoot如何優(yōu)雅的處理重復(fù)請求

    SpringBoot如何優(yōu)雅的處理重復(fù)請求

    對于一些用戶請求,在某些情況下是可能重復(fù)發(fā)送的,如果是查詢類操作并無大礙,但其中有些是涉及寫入操作的,一旦重復(fù)了,可能會導(dǎo)致很嚴(yán)重的后果,所以本文給大家介紹了SpringBoot優(yōu)雅的處理重復(fù)請求的方法,需要的朋友可以參考下
    2023-12-12
  • Java使用 try-with-resources 實現(xiàn)自動關(guān)閉資源的方法

    Java使用 try-with-resources 實現(xiàn)自動關(guān)閉資源的方法

    這篇文章主要介紹了Java使用 try-with-resources 實現(xiàn)自動關(guān)閉資源的方法,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • MyBatis-Plus?Page?分頁不生效的問題解決

    MyBatis-Plus?Page?分頁不生效的問題解決

    分頁是常見的一種功能,本文主要介紹了MyBatis-Plus?Page分頁不生效的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07
  • springboot多數(shù)據(jù)源配合docker部署mysql主從實現(xiàn)讀寫分離效果

    springboot多數(shù)據(jù)源配合docker部署mysql主從實現(xiàn)讀寫分離效果

    這篇文章主要介紹了springboot多數(shù)據(jù)源配合docker部署mysql主從實現(xiàn)讀寫分離,通過使用docker獲取mysql鏡像,具體內(nèi)容詳情跟隨小編一起看看吧
    2021-09-09
  • HTTP基本認(rèn)證(Basic Authentication)的JAVA實例代碼

    HTTP基本認(rèn)證(Basic Authentication)的JAVA實例代碼

    下面小編就為大家?guī)硪黄狧TTP基本認(rèn)證(Basic Authentication)的JAVA實例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • jdbc連接sqlserver數(shù)據(jù)庫示例

    jdbc連接sqlserver數(shù)據(jù)庫示例

    這篇文章主要介紹了jdbc連接sqlserver數(shù)據(jù)庫示例,需要的朋友可以參考下
    2014-04-04
  • Windows上安裝并使用SDKMAN詳細(xì)教程(JDK多版本管理)

    Windows上安裝并使用SDKMAN詳細(xì)教程(JDK多版本管理)

    在Linux和macOS上,開發(fā)者可以輕松使用SDKMAN來管理多個JDK版本,但Windows原生并不直接支持SDKMAN,這篇文章主要介紹了Windows上安裝并使用SDKMAN管理JDK多版本管理的相關(guān)資料,需要的朋友可以參考下
    2026-05-05
  • Java 使用Axis調(diào)用WebService的示例代碼

    Java 使用Axis調(diào)用WebService的示例代碼

    這篇文章主要介紹了Java 使用Axis調(diào)用WebService的示例代碼,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-09-09
  • 快速排序算法原理及java遞歸實現(xiàn)

    快速排序算法原理及java遞歸實現(xiàn)

    快速排序 對冒泡排序的一種改進(jìn),若初始記錄序列按關(guān)鍵字有序或基本有序,蛻化為冒泡排序。使用的是遞歸原理,在所有同數(shù)量級O(n longn) 的排序方法中,其平均性能最好。就平均時間而言,是目前被認(rèn)為最好的一種內(nèi)部排序方法
    2014-01-01

最新評論

茌平县| 常州市| 和政县| 平昌县| 宁陵县| 南安市| 新和县| 乌苏市| 遂川县| 新丰县| 平阴县| 奎屯市| 卓尼县| 清水河县| 定兴县| 丰原市| 临夏县| 江达县| 舟曲县| 和平县| 濮阳市| 景东| 观塘区| 岐山县| 始兴县| 密云县| 灵山县| 桦川县| 大关县| 玉山县| 万全县| 拉萨市| 绍兴县| 伊宁市| 宜春市| 定日县| 新巴尔虎右旗| 通山县| 苗栗市| 泗水县| 昆明市|