Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何仅对某些控制器使用弹簧转换器?_Java_Spring_Spring Boot_Spring Mvc_Converters - Fatal编程技术网

Java 如何仅对某些控制器使用弹簧转换器?

Java 如何仅对某些控制器使用弹簧转换器?,java,spring,spring-boot,spring-mvc,converters,Java,Spring,Spring Boot,Spring Mvc,Converters,我有一个c转换器,它可以工作: public class StringToLongConverter implements Converter<String, Long> { @Override public Long convert(String source) { Long myDecodedValue = ... return myDecodedValue; } } 一切都很好,但它适用于所有控制器,我需要它只执行一些控

我有一个c转换器,它可以工作:

public class StringToLongConverter implements Converter<String, Long> {
    @Override
    public Long convert(String source) {
        Long myDecodedValue = ...
        return myDecodedValue;
    }
}
一切都很好,但它适用于所有控制器,我需要它只执行一些控制器

//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);
}
//我需要此控制器从转换器获取myvalue
@RequestMapping(value=“{myvalue}”,method=RequestMethod.POST)
公共响应属性myvalue 1(@PathVariable Long myvalue){
返回新的响应状态(HttpStatus.OK);
}
//我需要这个控制器在没有转换器的情况下获取myvalue
@RequestMapping(value=“{myvalue}”,method=RequestMethod.POST)
公共响应属性myvalue 2(@PathVariable Long myvalue){
返回新的响应状态(HttpStatus.OK);
}

我们可以指定哪些转换器或参数应该与自定义转换器一起使用,哪些不应该使用吗?

通常来说,注册的
转换器绑定到输入源和输出目标。在您的情况下
。您使用的默认Spring转换器将在每个匹配的源-目标对上应用转换

为了更好地控制何时应用转换,可以使用。该接口包含3种方法:

  • 布尔匹配(TypeDescriptor sourceType、TypeDescriptor targetType)
    ,以确定是否应应用转换

  • 设置getConvertibleTypes()
    以返回一组可应用转换的源-目标对

  • 对象转换(对象源、类型描述符源类型、类型描述符目标类型)
    进行实际转换的方法

我已经建立了一个小型Spring项目来使用
ConditionalGenericConverter

RequiresConversion.java:

//RequiresConversion是本例中单独使用的自定义注释
//将属性注释为“可转换”的步骤
@目标(ElementType.PARAMETER)
@保留(RetentionPolicy.RUNTIME)
public@interface requireconversion{
}
SomeConverter.java:

@组件
公共类SomeConverter实现ConditionalGenericConverter{
@凌驾
公共布尔匹配(TypeDescriptor源类型、TypeDescriptor目标类型){
//验证注释是否存在
返回targetType.getAnnotation(RequiresConversion.class)!=null;
}
@凌驾
公共集getConvertibleTypes(){
返回Collections.singleton(新的ConvertiblePair(String.class,Long.class));
}
@凌驾
公共对象转换(对象源、类型描述符源类型、类型描述符目标类型){
//这里的转换逻辑
//在本例中,它从源字符串中删除“value”
字符串sourceValue=((字符串)source).replace(“value”,”);
返回Long.valueOf(sourceValue);
}
}
SomeController.java:

@RestController
公共类控制器{
//使用的路径变量将被转换,产生“值”-前缀
//在某个转换器中被剥离
//请注意自定义的“@RequiresConversion”注释
@GetMapping(value=“/test/{myvalue}”)
公共响应属性myvalue(@RequiresConversion@PathVariable Long myvalue){
返回新的响应状态(HttpStatus.OK);
}
//由于@RequiresConversion注释不存在,
//转换未应用于@PathVariable
@GetMapping(value=“/test2/{myvalue}”)
公共响应属性myvalue 2(@PathVariable Long myvalue){
返回新的响应状态(HttpStatus.OK);
}
}
转换将在上进行,产生
123
Long值。但是,由于第二个映射上不存在自定义注释
@RequiresConversion
,因此将跳过上的转换

您还可以通过将注释重命名为
SkipConversion
并验证
matches()
方法中是否缺少注释来反转注释


希望这有帮助

你是天才。非常感谢你的帮助!
//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);
}