如何使用 Spring 转换器仅用于某些控制器?
How to use Spring Converter for some controllers only?
我有可用的 c 转换器:
public class StringToLongConverter implements Converter<String, Long> {
@Override
public Long convert(String source) {
Long myDecodedValue = ...
return myDecodedValue;
}
}
在网络配置中我有:
@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()
到 return 一组可以应用转换的源-目标对
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
Long 值。但是,由于第二个映射中不存在自定义注释 @RequiresConversion
,因此将跳过 http://localhost:8080/test2/value123 上的转换。
您还可以通过将注释重命名为 SkipConversion
并在 matches()
方法中验证注释是否 absent 来反转注释。
希望对您有所帮助!
我有可用的 c 转换器:
public class StringToLongConverter implements Converter<String, Long> {
@Override
public Long convert(String source) {
Long myDecodedValue = ...
return myDecodedValue;
}
}
在网络配置中我有:
@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()
到 return 一组可以应用转换的源-目标对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
Long 值。但是,由于第二个映射中不存在自定义注释 @RequiresConversion
,因此将跳过 http://localhost:8080/test2/value123 上的转换。
您还可以通过将注释重命名为 SkipConversion
并在 matches()
方法中验证注释是否 absent 来反转注释。
希望对您有所帮助!