java如何仅对某些控制器使用Spring转换器?
我有一个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);
}
我们是否可以指定哪些转换器或参数应与自定义转换器一起使用,哪些不应使用
# 1 楼答案
通常来说,注册的
Converter
绑定到输入源和输出目标。在你的例子中<String, Long>
。您使用的默认Spring转换器将在每个匹配的源-目标对上应用转换为了更好地控制何时应用转换,可以使用^{} 。该接口包含3种方法:
boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType)
,以确定是否应应用转换Set<ConvertiblePair> getConvertibleTypes()
要返回一组源-目的地对,可将转换应用于Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType)
实际转换发生的方法我已经建立了一个小型Spring项目来使用
ConditionalGenericConverter
:安魂曲版本。java:
SomeConverter。java:
SomeController。java:
转换将在http://localhost:8080/test/value123上发生,从而产生一个
123
长的值。但是,由于第二个映射上不存在自定义注释@RequiresConversion
,因此将跳过http://localhost:8080/test2/value123上的转换您还可以通过将注释重命名为
SkipConversion
并验证该注释在matches()
方法中是否不存在来反转注释希望这有帮助