日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本文介紹了如何將Spring Converter僅用于某些控制器?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問題描述

我有工作正常的c轉(zhuǎn)換器:

public class StringToLongConverter implements Converter<String, Long> {
    @Override
    public Long convert(String source) {
        Long myDecodedValue = ...
        return myDecodedValue;
    }
}

在Web配置中,我有:

@Override
public void addFormatters (FormatterRegistry registry) {
    registry.addConverter(new StringToLongConverter());
}

一切都很好,但它對(duì)所有控制器都有效,我只需要對(duì)某些控制器執(zhí)行它。

//I need this controller to get myvalue from converter
@RequestMapping(value = "{myvalue}", method = RequestMethod.POST)
public ResponseEntity myvalue1(@PathVariable Long myvalue) {

    return new ResponseEntity<>(HttpStatus.OK);
}

//I need this controller to get myvalue without converter
@RequestMapping(value = "{myvalue}", method = RequestMethod.POST)
public ResponseEntity myvalue2(@PathVariable Long myvalue) {

    return new ResponseEntity<>(HttpStatus.OK);
}

我們是否可以指定哪些轉(zhuǎn)換器或參數(shù)應(yīng)該與自定義轉(zhuǎn)換器一起使用,哪些不應(yīng)該?

推薦答案

一般來(lái)說(shuō),注冊(cè)的Converter綁定輸入源和輸出目的地。在您的情況下<String, Long>。您使用的默認(rèn)Spring轉(zhuǎn)換器將在每個(gè)匹配源-目標(biāo)對(duì)上應(yīng)用轉(zhuǎn)換。

要更好地控制何時(shí)應(yīng)用轉(zhuǎn)換,可以使用ConditionalGenericConverter。接口包含3個(gè)方法:

boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType),以確定是否應(yīng)應(yīng)用轉(zhuǎn)換

Set<ConvertiblePair> getConvertibleTypes()若要返回一組源-目標(biāo)對(duì),可以將轉(zhuǎn)換應(yīng)用于

Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType)進(jìn)行實(shí)際轉(zhuǎn)換的方法。

我已經(jīng)設(shè)置了一個(gè)小的Spring項(xiàng)目來(lái)使用ConditionalGenericConverter

RequiresConversion.java:

// RequiresConversion is a custom annotation solely used in this example
// to annotate an attribute as "convertable"
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiresConversion {
}

SomeConverter.java:

@Component
public class SomeConverter implements ConditionalGenericConverter {

    @Override
    public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
        // Verify whether the annotation is present
        return targetType.getAnnotation(RequiresConversion.class) != null;
    }

    @Override
    public Set<ConvertiblePair> getConvertibleTypes() {
        return Collections.singleton(new ConvertiblePair(String.class, Long.class));
    }

    @Override
    public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
        // Conversion logic here
        // In this example it strips "value" from the source string
        String sourceValue = ((String) source).replace("value", "");
        return Long.valueOf(sourceValue);
    }
}

SomeController.java:

@RestController
public class SomeController {

    // The path variable used will be converted, resulting in the "value"-prefix 
    // being stripped in SomeConverter
    // Notice the custom '@RequiresConversion' annotation
    @GetMapping(value = "/test/{myvalue}")
    public ResponseEntity myvalue(@RequiresConversion @PathVariable Long myvalue) {
        return new ResponseEntity<>(HttpStatus.OK);
    }

    // As the @RequiresConversion annotation is not present,
    // the conversion is not applied to the @PathVariable
    @GetMapping(value = "/test2/{myvalue}")
    public ResponseEntity myvalue2(@PathVariable Long myvalue) {
        return new ResponseEntity<>(HttpStatus.OK);
    }
}

轉(zhuǎn)換將在http://localhost:8080/test/value123進(jìn)行,從而產(chǎn)生123長(zhǎng)值。但是,由于第二個(gè)映射中不存在自定義批注@RequiresConversion,因此將跳過http://localhost:8080/test2/value123上的轉(zhuǎn)換。

您還可以通過將批注重命名為SkipConversion并驗(yàn)證該批注是否在matches()方法中不存在來(lái)反轉(zhuǎn)批注。

希望這能有所幫助!

這篇關(guān)于如何將Spring Converter僅用于某些控制器?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,

分享到:
標(biāo)簽:Converter Spring 如何將 控制器 用于
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定