我想使用Spring xd从Web服务使用HTTP/1.1协议来获取文本/ XML响应。目标是将xml响应转换为json并插入到mongodb中。这里是我的春天module.xml春季集成轮询HTTP/1.1 Web服务文本/ 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>
和我的测试类
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 '[email protected]41439f'
[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 [[[email protected] testClass = UrlStreamTest, locations = array<String>['classpath:/com/bosch/test/UrlStreamTest-context.xml'], testInstance = [email protected], testMethod = [email protected], testException = java.lang.AssertionError: ]], dirtiesContext [false].
[2015-10-15 16:09:26.053] log4j - ???? INFO [Thread-0] --- GenericApplicationContext: Closing [email protected]41439f: 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变压器空为好。我可以在浏览器中打开网址并获得回复。我假设一旦我可以得到一条消息,我将不得不创建一个入站通道适配器,如在这篇文章中 Spring Integration: Poll HTTP ,但我目前无法收到单个响应。
启用跟踪日志记录org.springframework.integration后,我看这是我的系统输出语句
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
我没有看到您的测试发送任何输入。您应该通过为'org.springframework.integration'启用DEBUG日志来看到消息流。 –
从单步执行代码看起来,对网址的请求应该已经发送并将响应放入队列中? ouput.receive()试图让一个空队列出队 – gary69
你在哪里发送请求到URL?这不在你的测试中。你所做的所有测试都是启动上下文并等待接收()一条消息。正如我所说,当消息被发送时(直接输入或通过http适配器),你会看到消息记录('preSend on channel'input''等)。您的流有'时间'来源,但您的测试不会发送任何数据。 –