@Component public class FeignDateFormatRegister implements FeignFormatterRegistrar { public FeignDateFormatRegister() { } @Override public void registerFormatters(FormatterRegistry registry) { registry.addConverter(Date.class, String.class, new Date2StringConverter()); } private class Date2StringConverter implements Converter<Date, String> { @Override public String convert(Date source) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(source); } } }
时间: 2024-02-26 21:55:07 浏览: 22
这是一个实现了 FeignFormatterRegistrar 接口的类,它的作用是注册一个自定义的 Converter,用于将 Date 类型的参数转换成 String 类型。在这个类中,我们实现了 registerFormatters 方法,并将自定义的 Converter 注册到了 FormatterRegistry 中。在 Date2StringConverter 中,我们重写了 convert 方法,将 Date 类型的参数转换成了符合指定格式的 String 类型。这个类的作用是为 Feign 的请求提供一个自定义的日期格式化方式。使用这个类可以方便地实现对 Feign 请求中的日期类型参数的格式化。
相关问题
package com.yfyy.zero.mktquot.core.config; import org.springframework.cloud.openfeign.FeignFormatterRegistrar; import org.springframework.core.convert.converter.Converter; import org.springframework.format.FormatterRegistry; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; @Component public class FeignDateFormatRegister implements FeignFormatterRegistrar { public FeignDateFormatRegister() { } @Override public void registerFormatters(FormatterRegistry registry) { registry.addConverter(Date.class, String.class, new Date2StringConverter()); } private class Date2StringConverter implements Converter<Date, String> { @Override public String convert(Date source) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(source); } } }
这是一个实现了 FeignFormatterRegistrar 接口的类,名为 FeignDateFormatRegister。它的作用是注册一个自定义的 Converter,用于将 Date 类型的参数转换成 String 类型。在这个类中,我们实现了 registerFormatters 方法,并将自定义的 Converter 注册到了 FormatterRegistry 中。在 Date2StringConverter 中,我们重写了 convert 方法,将 Date 类型的参数转换成了符合指定格式的 String 类型。这个类的作用是为 Feign 的请求提供一个自定义的日期格式化方式。使用这个类可以方便地实现对 Feign 请求中的日期类型参数的格式化。
@Slf4j @Component public class FMSGCallBack implements HCNetSDK.FMSGCallBack_V31 { @Autowired private RedisService redisService; redisService 报空指针异常
这个问题可能是因为你没有将 RedisService 注入到 FMSGCallBack 类中。你可以检查一下 FMSGCallBack 类是否被正确地注入到 Spring 容器中,并且 RedisService 是否被正确地注入到 FMSGCallBack 类中。
另外,如果你要在非 Spring 管理的类中使用 Spring 管理的 Bean,你可以通过 ApplicationContext 的 getBean 方法获取 Bean 对象,而不是使用 @Autowired 注解注入。可以尝试一下这个方法:
```
@Slf4j
@Component
public class FMSGCallBack implements HCNetSDK.FMSGCallBack_V31 {
private RedisService redisService;
@Override
public void invoke(NativeLong lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
// 通过 ApplicationContext 获取 RedisService Bean
ApplicationContext context = SpringContextUtil.getApplicationContext();
redisService = context.getBean(RedisService.class);
// 使用 redisService 对象进行操作
redisService.xxx();
}
}
```
这里通过 SpringContextUtil 工具类获取了 ApplicationContext,然后通过 ApplicationContext 获取了 RedisService Bean 对象,最终使用这个对象进行操作。注意,这里需要将 RedisService 类型传入 getBean 方法中。