公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

本文介绍了如何将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仅用于某些控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

分享到:
标签:Converter Spring 如何将 控制器 用于
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定