将自定义类型转换器添加到 Spring XD

Adding a custom Type Converter to Spring XD

不幸的是,Spring XD 似乎没有附带 XML 类型转换器 (application/xml)。

我实现了 2 AbstractFromMessageConverters 将 POJO 转换为 XML,反之亦然。

根据documentation it says to look at streams.xml where I think they were really referring to this XML。我提供了一个名为 customMessageConverters 的列表和我自己的配置,它公开了这些转换器,试图将它们添加到 Spring XD 应用程序上下文。

我写了:

@Configuration
    @Slf4j
    public class XmlConverterConfiguration {

        @Bean
        public CompositeMessageConverter compositeMessageConverter(final  MessageConverter[] converters) {
                log.info("Registering XML converters");
                return new CompositeMessageConverter(Arrays.asList(converters));
            }
        }

并且我向我的转换器添加了 @Component 注释。

还有这个XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <!-- Users can override this to add converters.-->
    <util:list id="customMessageConverters">
        <bean class="my.custom.mime.converter.PojoToXmlMessageConverter"/>
        <bean class="my.custom.mime.converter.XmlToPojoMessageConverter"/>
    </util:list>

</beans>

我尝试将此 XML 添加到 src/main/resourcessrc/main/resources/config 然后我将编译的 .jar 放入 $XD_HOME/lib 但它没有正确注册。

我没有从我的配置中看到日志,当我尝试将 outputType 设置为 application/xml 时,我得到一个不支持的异常:

org.springframework.xd.dirt.plugins.ModuleConfigurationException: Content type is not supported for outputType=application/xml

我哪里做错了?

--编辑--

我还在 server.yml 中取消注释这些行,并将基础包更改为我的。

    ---
# User Extensions: Where XD scans the classpath to discover extended container configuration to add beans to the Plugins context.
# Each property may be a comma delimited string. 'basepackages' refers to package names used for
# annotated component (@Configuration or @Component stereotypes) scanning. 'locations' is a list of root resource directories containing XML or Groovy configuration.
# XD prepends classpath:* if no prefix included and appends **/*.* to each location
xd:
  extensions:
      basepackages: my.custom.mime.converter
      locations: META-INF/spring-xd/ext
---

您必须将文件放在一个狭窄的文件夹中,例如/myCustomConverters 在罐子里;然后你必须通过取消注释 serverys.yml ...

中的配置来告诉 XD 如何找到它
# User Extensions: Where XD scans the classpath to discover extended container configuration to add beans to the Plugins context.
# Each property may be a comma delimited string. 'basepackages' refers to package names used for
# annotated component (@Configuration or @Component stereotypes) scanning. 'locations' is a list of root resource directories containing XML or Groovy configuration.
# XD prepends classpath:* if no prefix included and appends **/*.* to each location
xd:
  extensions:
#      basepackages: com.acme.xd.extensions
#      locations: META-INF/spring-xd/ext
      locations: myCustomConverters