• <bdo id='m8slF'></bdo><ul id='m8slF'></ul>
  • <i id='m8slF'><tr id='m8slF'><dt id='m8slF'><q id='m8slF'><span id='m8slF'><b id='m8slF'><form id='m8slF'><ins id='m8slF'></ins><ul id='m8slF'></ul><sub id='m8slF'></sub></form><legend id='m8slF'></legend><bdo id='m8slF'><pre id='m8slF'><center id='m8slF'></center></pre></bdo></b><th id='m8slF'></th></span></q></dt></tr></i><div id='m8slF'><tfoot id='m8slF'></tfoot><dl id='m8slF'><fieldset id='m8slF'></fieldset></dl></div>

    1. <tfoot id='m8slF'></tfoot>

      <legend id='m8slF'><style id='m8slF'><dir id='m8slF'><q id='m8slF'></q></dir></style></legend>

        <small id='m8slF'></small><noframes id='m8slF'>

      1. 如何在 Spring Boot 中注册自定义转换器?

        How to register custom converters in spring boot?(如何在 Spring Boot 中注册自定义转换器?)

        1. <legend id='JnyhL'><style id='JnyhL'><dir id='JnyhL'><q id='JnyhL'></q></dir></style></legend>
          <i id='JnyhL'><tr id='JnyhL'><dt id='JnyhL'><q id='JnyhL'><span id='JnyhL'><b id='JnyhL'><form id='JnyhL'><ins id='JnyhL'></ins><ul id='JnyhL'></ul><sub id='JnyhL'></sub></form><legend id='JnyhL'></legend><bdo id='JnyhL'><pre id='JnyhL'><center id='JnyhL'></center></pre></bdo></b><th id='JnyhL'></th></span></q></dt></tr></i><div id='JnyhL'><tfoot id='JnyhL'></tfoot><dl id='JnyhL'><fieldset id='JnyhL'></fieldset></dl></div>

          <small id='JnyhL'></small><noframes id='JnyhL'>

            <tbody id='JnyhL'></tbody>

              <bdo id='JnyhL'></bdo><ul id='JnyhL'></ul>

                • <tfoot id='JnyhL'></tfoot>
                • 本文介绍了如何在 Spring Boot 中注册自定义转换器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我使用 spring-boot-starter-jdbc (v1.3.0) 编写应用程序.

                  I writing application using spring-boot-starter-jdbc (v1.3.0).

                  我遇到的问题:BeanPropertyRowMapper 的实例失败,因为它无法从 java.sql.Timestamp 转换为 java.time.LocalDateTime.

                  The problem that I met: Instance of BeanPropertyRowMapper fails as it cannot convert from java.sql.Timestamp to java.time.LocalDateTime.

                  为了复制这个问题,我实现了org.springframework.core.convert.converter.Converter 用于这些类型.

                  In order to copy this problem, I implemented org.springframework.core.convert.converter.Converter for these types.

                  public class TimeStampToLocalDateTimeConverter implements Converter<Timestamp, LocalDateTime> {
                  
                      @Override
                      public LocalDateTime convert(Timestamp s) {
                          return s.toLocalDateTime();
                      }
                  }
                  

                  我的问题是:如何为 BeanPropertyRowMapper 提供 TimeStampToLocalDateTimeConverter.

                  My question is: How do I make available TimeStampToLocalDateTimeConverter for BeanPropertyRowMapper.

                  更一般的问题,我如何注册我的转换器,以使它们在系统范围内可用?

                  More general question, how do I register my converters, in order to make them available system wide?

                  以下代码将我们带到初始化阶段的NullPointerException:

                  The following code bring us to NullPointerException on initialization stage:

                  private Set<Converter> getConverters() {
                      Set<Converter> converters = new HashSet<Converter>();
                      converters.add(new TimeStampToLocalDateTimeConverter());
                      converters.add(new LocalDateTimeToTimestampConverter());
                  
                      return converters;
                  }
                  
                  @Bean(name="conversionService")
                  public ConversionService getConversionService() {
                      ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
                      bean.setConverters(getConverters()); 
                      bean.afterPropertiesSet();
                      return bean.getObject();
                  }    
                  

                  谢谢.

                  推荐答案

                  所有自定义转换服务都必须注册到 FormatterRegistry.尝试通过实现 WebMvcConfigurer 来创建新配置并注册转换服务

                  All custom conversion service has to be registered with the FormatterRegistry. Try creating a new configuration and register the conversion service by implementing the WebMvcConfigurer

                  @Configuration
                  public class WebConfig implements WebMvcConfigurer {
                  
                      @Override
                      public void addallFormatters(FormatterRegistry registry) {
                          registry.addConverter(new TimeStampToLocalDateTimeConverter());
                      }
                  }
                  

                  希望这可行.

                  这篇关于如何在 Spring Boot 中注册自定义转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                  <small id='EF0k6'></small><noframes id='EF0k6'>

                    <tbody id='EF0k6'></tbody>
                      <i id='EF0k6'><tr id='EF0k6'><dt id='EF0k6'><q id='EF0k6'><span id='EF0k6'><b id='EF0k6'><form id='EF0k6'><ins id='EF0k6'></ins><ul id='EF0k6'></ul><sub id='EF0k6'></sub></form><legend id='EF0k6'></legend><bdo id='EF0k6'><pre id='EF0k6'><center id='EF0k6'></center></pre></bdo></b><th id='EF0k6'></th></span></q></dt></tr></i><div id='EF0k6'><tfoot id='EF0k6'></tfoot><dl id='EF0k6'><fieldset id='EF0k6'></fieldset></dl></div>

                        1. <legend id='EF0k6'><style id='EF0k6'><dir id='EF0k6'><q id='EF0k6'></q></dir></style></legend>
                          • <bdo id='EF0k6'></bdo><ul id='EF0k6'></ul>
                            <tfoot id='EF0k6'></tfoot>