feignclient请求参数为LocalDate时输出现象解析

1、简介

在使用feignclient来发送请求时,请求参数为LocalDate类型时,请求时url上的日期格式为yy-MM-dd。为什么会这样显示呢?

2、分析

feignclient请求参数处理时通过SpringMvcContract#processAnnotationsOnParameter在处理方法参数上的注解时,会根据请求参数得到对应的Expander,用于转为字符串。Expander接口定义为

public interface Expander {
    String expand(Object var1);
}

对应处理代码为

@Override
protected boolean processAnnotationsOnParameter(MethodMetadata data,
		Annotation[] annotations, int paramIndex) {
	boolean isHttpAnnotation = false;

	AnnotatedParameterProcessor.AnnotatedParameterContext context = new SimpleAnnotatedParameterContext(
			data, paramIndex);
	Method method = processedMethods.get(data.configKey());
	for (Annotation parameterAnnotation : annotations) {
		AnnotatedParameterProcessor processor = annotatedArgumentProcessors
				.get(parameterAnnotation.annotationType());
		if (processor != null) {
			Annotation processParameterAnnotation;
			// synthesize, handling @AliasFor, while falling back to parameter name on
			// missing String #value():
			processParameterAnnotation = synthesizeWithMethodParameterNameAsFallbackValue(
					parameterAnnotation, method, paramIndex);
			isHttpAnnotation |= processor.processArgument(context,
					processParameterAnnotation, method);
		}
	}

	if (!isMultipartFormData(data) && isHttpAnnotation
			&& data.indexToExpander().get(paramIndex) == null) {
		TypeDescriptor typeDescriptor = createTypeDescriptor(method, paramIndex);
		if (conversionService.canConvert(typeDescriptor, STRING_TYPE_DESCRIPTOR)) {
			Param.Expander expander = convertingExpanderFactory
					.getExpander(typeDescriptor);
			if (expander != null) {
				data.indexToExpander().put(paramIndex, expander);
			}
		}
	}
	return isHttpAnnotation;
}

通过convertingExpanderFactory.getExpander(typeDescriptor)获取到参数对应TypeDescriptor的Expander。

convertingExpanderFactory是在什么时候初始化的呢?

是通过构造函数来初始化的。

 在何处会调用这个构造函数呢?

在FeignClientsConfiguration文件中通过@Bean注解来调用的。

 构造函数中的feignConversionService是什么时候构造的?

也是在FeignClientsConfiguration文件中通过@Bean注解来调用的,创建DefaultFormattingConversionService,如果有自定义FeignFormatterRegistrar,则会添加对应的Formatter。

 FeignFormatterRegistrar是接口,继承自FormatterRegistrar接口。

 通过以上分析,要实现自定义LocalDate到字符串的转换,可以自定义类实现FeignFormatterRegistrar。

3、解决方案

自定义类实现FeignFormatterRegistrar

@Component
public class LocalDateFeignFormatterRegistrar implements FeignFormatterRegistrar {
	@Override
	public void registerFormatters(FormatterRegistry registry) {
		registry.addConverter(LocalDate.class, String.class, new Converter<LocalDate, String>() {
			@Override
			public String convert(LocalDate source) {
				return LocalDateTimeUtil.format(source, DatePattern.NORM_DATETIME_PATTERN);
        }
		});
	}
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值