本文介紹了如何將Spring Converter僅用于某些控制器?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有工作正常的c轉換器:
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());
}
一切都很好,但它對所有控制器都有效,我只需要對某些控制器執行它。
//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);
}
我們是否可以指定哪些轉換器或參數應該與自定義轉換器一起使用,哪些不應該?
推薦答案
一般來說,注冊的Converter
綁定輸入源和輸出目的地。在您的情況下<String, Long>
。您使用的默認Spring轉換器將在每個匹配源-目標對上應用轉換。
要更好地控制何時應用轉換,可以使用ConditionalGenericConverter
。接口包含3個方法:
boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType)
,以確定是否應應用轉換
Set<ConvertiblePair> getConvertibleTypes()
若要返回一組源-目標對,可以將轉換應用于
Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType)
進行實際轉換的方法。
我已經設置了一個小的Spring項目來使用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);
}
}
轉換將在http://localhost:8080/test/value123進行,從而產生123
長值。但是,由于第二個映射中不存在自定義批注@RequiresConversion
,因此將跳過http://localhost:8080/test2/value123上的轉換。
您還可以通過將批注重命名為SkipConversion
并驗證該批注是否在matches()
方法中不存在來反轉批注。
希望這能有所幫助!
這篇關于如何將Spring Converter僅用于某些控制器?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,