Spring 集成投票 HTTP/1.1 网络服务 text/xml

Spring Integration poll HTTP/1.1 web service text/xml

我正在尝试使用 spring xd 从使用 HTTP/1.1 协议的 Web 服务获取 text/xml 响应。目标是将 xml 响应转换为 json 并插入 mongodb。这是我的 spring-module.xml

<beans>
        <int-http:outbound-gateway id='HTTPGateway'
              request-channel='input'
              url-expression="'${protocol}://${host}'+'/${context}'"
              http-method='GET'
              expected-response-type='${expectedresponsetype}'
              charset='UTF-8'
              reply-timeout='${replytimeout}'
              reply-channel='output'>    
         </int-http:outbound-gateway>

         <int:channel id="input"/>

        <int:transformer input-channel="to.json" output-channel="output">
            <bean class="com.modules.JsonMapper"/>
        </int:transformer>

        <int:channel id="output"/>


    </beans>

和我的context.xml

<beans>    
    <context:property-placeholder properties-ref="props"/>
    <util:properties id="props">
        <prop key="expectedresponsetype">text/xml</prop>
        <prop key="replytimeout">5000</prop>
        <prop key="host">www.ctabustracker.com</prop>
        <prop key="protocol">http</prop>
        <prop key="context">bustime/api/v2/getvehicles?key=key%26vid=1</prop>   
    </util:properties>

    <import resource="classpath:config/spring-module.xml"/>

    <!-- Override direct channel with a queue channel so the test will block until a message is received -->
    <int:channel id="output">
        <int:queue/>
    </int:channel>
</beans>

和我的测试class

import org.springframework.messaging.Message;
import org.springframework.messaging.PollableChannel;
//import org.springframework.integration.channel.PollableChannel;
//import org.springframework.integration.core.Message;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class UrlStreamTest {

    @Autowired
    PollableChannel output;

    @Autowired
    ConfigurableApplicationContext applicationContext;

    Logger log = Logger.getLogger(UrlStreamTest.class);

    @Test
    public void test() {
        applicationContext.start();
        System.out.println("About to receive message");
        Message message = output.receive(10000);
        System.out.println("Received message: " + message);
        assertNotNull(message);
        //log.debug(message.getPayload());
        assertTrue(message.getPayload() instanceof String);
    }
}

当我 运行 测试时,我收到的消息为空,但我没有收到任何异常。我配置了 log4j,下面是输出的片段

[2015-10-15 16:09:16.007] log4j - ???? DEBUG [main] --- InjectionMetadata: Processing injected element of bean 'com.bosch.test.UrlStreamTest': AutowiredFieldElement for org.springframework.messaging.PollableChannel com.bosch.test.UrlStreamTest.output
[2015-10-15 16:09:16.009] log4j - ???? DEBUG [main] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'output'
[2015-10-15 16:09:16.009] log4j - ???? DEBUG [main] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'nullChannel'
[2015-10-15 16:09:16.010] log4j - ???? DEBUG [main] --- AutowiredAnnotationBeanPostProcessor: Autowiring by type from bean name 'com.bosch.test.UrlStreamTest' to bean named 'output'
[2015-10-15 16:09:16.011] log4j - ???? DEBUG [main] --- InjectionMetadata: Processing injected element of bean 'com.bosch.test.UrlStreamTest': AutowiredFieldElement for org.springframework.context.ConfigurableApplicationContext com.bosch.test.UrlStreamTest.applicationContext
[2015-10-15 16:09:16.011] log4j - ???? DEBUG [main] --- AutowiredAnnotationBeanPostProcessor: Autowiring by type from bean name 'com.bosch.test.UrlStreamTest' to bean named 'org.springframework.context.support.GenericApplicationContext@4641439f'
[2015-10-15 16:09:16.013] log4j - ???? DEBUG [main] --- ProfileValueUtils: Retrieved @ProfileValueSourceConfiguration [null] for test class [class com.bosch.test.UrlStreamTest].
[2015-10-15 16:09:16.014] log4j - ???? DEBUG [main] --- ProfileValueUtils: Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [class com.bosch.test.UrlStreamTest].
[2015-10-15 16:09:16.020] log4j - ???? DEBUG [main] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'integrationHeaderChannelRegistry'
[2015-10-15 16:09:16.020] log4j - ???? DEBUG [main] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'HTTPGateway'
[2015-10-15 16:09:16.020] log4j - ???? DEBUG [main] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.integration.config.ConsumerEndpointFactoryBean#0'
[2015-10-15 16:09:16.020] log4j - ???? DEBUG [main] --- DefaultListableBeanFactory: Returning cached instance of singleton bean '_org.springframework.integration.errorLogger'
[2015-10-15 16:09:16.020] log4j - ???? DEBUG [main] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'lifecycleProcessor'
[2015-10-15 16:09:16.020] log4j - ????  INFO [main] --- DefaultLifecycleProcessor: Starting beans in phase 0
[2015-10-15 16:09:16.020] log4j - ???? DEBUG [main] --- DefaultLifecycleProcessor: Starting bean 'integrationHeaderChannelRegistry' of type [class org.springframework.integration.channel.DefaultHeaderChannelRegistry]
[2015-10-15 16:09:16.023] log4j - ???? DEBUG [main] --- DefaultLifecycleProcessor: Successfully started bean 'integrationHeaderChannelRegistry'
[2015-10-15 16:09:16.023] log4j - ???? DEBUG [main] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.integration.config.IdGeneratorConfigurer#0'
About to receive message
Received message: null
[2015-10-15 16:09:26.045] log4j - ???? DEBUG [main] --- SpringMethodRoadie: Test method [public void com.bosch.test.UrlStreamTest.test()] threw exception [java.lang.AssertionError: ].
[2015-10-15 16:09:26.047] log4j - ???? DEBUG [main] --- DirtiesContextTestExecutionListener: After test method: context [[TestContext@450faed3 testClass = UrlStreamTest, locations = array<String>['classpath:/com/bosch/test/UrlStreamTest-context.xml'], testInstance = com.bosch.test.UrlStreamTest@89ec211, testMethod = test@UrlStreamTest, testException = java.lang.AssertionError: ]], dirtiesContext [false].
[2015-10-15 16:09:26.053] log4j - ????  INFO [Thread-0] --- GenericApplicationContext: Closing org.springframework.context.support.GenericApplicationContext@4641439f: startup date [Thu Oct 15 16:09:15 MDT 2015]; root of context hierarchy
[2015-10-15 16:09:26.054] log4j - ???? DEBUG [Thread-0] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.integration.config.IdGeneratorConfigurer#0'
[2015-10-15 16:09:26.056] log4j - ???? DEBUG [Thread-0] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'integrationHeaderChannelRegistry'
[2015-10-15 16:09:26.056] log4j - ???? DEBUG [Thread-0] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'HTTPGateway'
[2015-10-15 16:09:26.056] log4j - ???? DEBUG [Thread-0] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'org.springframework.integration.config.ConsumerEndpointFactoryBean#0'
[2015-10-15 16:09:26.056] log4j - ???? DEBUG [Thread-0] --- DefaultListableBeanFactory: Returning cached instance of singleton bean '_org.springframework.integration.errorLogger'
[2015-10-15 16:09:26.056] log4j - ???? DEBUG [Thread-0] --- DefaultListableBeanFactory: Returning cached instance of singleton bean 'lifecycleProcessor'
[2015-10-15 16:09:26.057] log4j - ????  INFO [Thread-0] --- DefaultLifecycleProcessor: Stopping beans in phase 0
[2015-10-15 16:09:26.058] log4j - ???? DEBUG [Thread-0] --- DefaultLifecycleProcessor: Stopping bean 'integrationHeaderChannelRegistry' of type [class org.springframework.integration.channel.DefaultHeaderChannelRegistry]
[2015-10-15 16:09:26.058] log4j - ???? DEBUG [Thread-0] --- DefaultLifecycleProcessor: Successfully stopped bean 'integrationHeaderChannelRegistry'
[2015-10-15 16:09:26.058] log4j - ???? DEBUG [Thread-0] --- DefaultLifecycleProcessor: Asking bean 'HTTPGateway' of type [class org.springframework.integration.config.ConsumerEndpointFactoryBean] to stop

我找不到任何关于为什么消息为空的线索,没有 json 转换器也是空的。我可以在浏览器中点击 url 并获得响应。我假设一旦我收到一条消息,我就必须像这样创建一个入站通道适配器 post 但我现在无法收到任何回复。

为 org.springframework.integration 启用 TRACE 日志记录后,我在 sysout 语句之间看到了这一点

About to receive message
[2015-10-16 09:57:54.748] log4j - ???? TRACE [main] --- QueueChannel: preReceive on channel 'output'
[2015-10-16 09:58:04.750] log4j - ???? TRACE [main] --- QueueChannel: postReceive on channel 'output', message is null
Received message: null

您要将请求发送到哪里 URL?它不在你的测试中。您的所有测试所做的就是启动上下文并等待 receive() 一条消息。正如我所说,发送消息时(直接输入或通过 http 适配器),您将看到记录的消息(preSend on channel 'input' 等)。您的流有一个 'time' 来源,但您的测试没有发送任何数据。

http 网关是一个 request/response - 您必须通过向输入通道发送消息来向它发送请求。 @Autowire 输入和

input.send(new GenericMessage<String>(""));