本文介紹了如何將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ì)大家有所幫助,