Java interceptSendToEndpoint未使用Camel和Springboot拦截路由中的http请求
我正在尝试为我的路由编写一个单元测试,它接收一条消息,进行一个外部webservice调用,然后处理响应。然而,我似乎不能嘲笑外部Web服务调用。我尝试了以下方法:Java interceptSendToEndpoint未使用Camel和Springboot拦截路由中的http请求,java,spring-boot,junit,apache-camel,Java,Spring Boot,Junit,Apache Camel,我正在尝试为我的路由编写一个单元测试,它接收一条消息,进行一个外部webservice调用,然后处理响应。然而,我似乎不能嘲笑外部Web服务调用。我尝试了以下方法: @RunWith(CamelSpringBootRunner.class) @SpringBootTest @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) @MockEndpoints @UseAdviceWith publi
@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@DirtiesContext(classMode =
DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints
@UseAdviceWith
public class TypeServiceRoutersTest
{
@Autowired
private CamelContext camelContext;
@Autowired
private GetTypesProcess getTypesProcess;
@Produce(uri = "direct:start")
private ProducerTemplate incomingJmsRequestMessage;
@EndpointInject(uri = "mock:JMSReplyTo")
private MockEndpoint jmsReplyTo;
@Before
public void setUp()throws Exception{
camelContext.getRouteDefinition("Build XML Response").adviceWith(camelContext,
new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveAddLast().to("mock:JMSReplyTo");
}
});
camelContext.getRouteDefinition("Build & Send Http Request").adviceWith(camelContext,
new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
interceptSendToEndpoint("http://*")
.skipSendToOriginalEndpoint()
.setBody("Hello");
camelContext.start();
}
@Test
@DirtiesContext
public void RouteFlowTest() throws Exception{
Map<String,Object> jmsHeaders = new HashMap<>();
jmsHeaders.put("Auth","helloWorld");
jmsHeaders.put("JMSReplyTo","sample");
String jmsBody = "Hi"
incomingJmsRequestMessage.sendBodyAndHeaders("direct:start", jmsBody, jmsHeaders);
jmsReplyTo.expectedMessageCount(1);
jmsReplyTo.assertIsSatisfied();
}
@After
public void cleanUpCamelContext() throws Exception{
camelContext.stop();
}
interceptSendToEndpoint("http://.*")
但当我运行测试时,对webservice的调用仍然会运行:
.to("{{http.endpoints.GetTypes}}")
interceptSendToEndpoint("http://.*")
由于来自webservice的403错误,导致我的测试失败。我不确定我做错了什么
interceptSendToEndpoint("http://.*")
日志说明路由已更改,但也显示已调用端点:
2019-04-30 14:44:13.938 INFO 1007 --- [ main] org.apache.camel.model.RouteDefinition : Adviced route before/after as XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<route xmlns="http://camel.apache.org/schema/spring" customId="true" id="Build Send Http Request">
<from uri="direct:start"/>
<onException>
<log loggingLevel="INFO" message="${exception.stacktrace}"/>
<bean method="processFailure"/>
</onException>
<bean method="processRequest"/>
<log loggingLevel="DEBUG" message="Processed Request and built http request"/>
<to uri="http://help.co.za:10108"/>
<to uri="direct:processResponse"/>
</route>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<route xmlns="http://camel.apache.org/schema/spring" customId="true" id="Build Send Http Request">
<from uri="direct:start"/>
<onException>
<log loggingLevel="INFO" message="${exception.stacktrace}"/>
<bean method="processFailure"/>
</onException>
<interceptSendToEndpoint skipSendToOriginalEndpoint="true" uri="http*">
<setBody>
<simple>Hello</simple>
</setBody>
</interceptSendToEndpoint>
<bean method="processRequest"/>
<log loggingLevel="DEBUG" message="Processed Request and built http request"/>
<to uri="http://help.co.za:10108"/>
<to uri="direct:processResponse"/>
</route>
2019-04-30 14:44:13.940 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : Apache Camel 2.23.2 (CamelContext: camel-1) is starting
2019-04-30 14:44:13.941 INFO 1007 --- [ main] o.a.c.m.ManagedManagementStrategy : JMX is enabled
2019-04-30 14:44:14.120 INFO 1007 --- [ main] .c.i.InterceptSendToMockEndpointStrategy : Adviced endpoint [http://help.co.za:10108] with mock endpoint [mock:http:help.co.za:10108]
2019-04-30 14:44:14.125 INFO 1007 --- [ main] .c.i.InterceptSendToMockEndpointStrategy : Adviced endpoint [direct://processResponse] with mock endpoint [mock:direct:processResponse]
2019-04-30 14:44:14.151 INFO 1007 --- [ main] .c.i.InterceptSendToMockEndpointStrategy : Adviced endpoint [jms://queue.sample?exchangePattern=InOut] with mock endpoint [mock:jms:queue.sample]
2019-04-30 14:44:14.165 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
2019-04-30 14:44:14.259 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : Route: Build Send Http Request started and consuming from: direct://start
2019-04-30 14:44:14.259 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : Route: Build XML Response started and consuming from: direct://processResponse
2019-04-30 14:44:14.291 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : Route: Receive JMS Message started and consuming from: jms://queue.sample?exchangePattern=InOut
2019-04-30 14:44:14.293 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : Route: route1 started and consuming from: direct://jmsMessage
2019-04-30 14:44:14.294 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : Route: route2 started and consuming from: direct://jsonResponse
2019-04-30 14:44:14.294 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : Total 5 routes, of which 5 are started
2019-04-30 14:44:14.295 INFO 1007 --- [ main] o.a.camel.spring.SpringCamelContext : Apache Camel 2.23.2 (CamelContext: camel-1) started in 0.355 seconds
2019-04-30 14:44:14.478 INFO 1007 --- [ main] c.i.o.r.OP_GetTypes.TypeServiceRouter : org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking http://help.co.za:10108/ with statusCode: 404
at org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:273)```
interceptSendToEndpoint("http://.*")
2019-04-30 14:44:13.938信息1007---[main]org.apache.camel.model.routedDefinition:作为XML的前/后通知路线:
你好
2019-04-30 14:44:13.940信息1007---[main]o.a.camel.spring.SpringCamelContext:apachecamel 2.23.2(CamelContext:camel-1)正在启动
2019-04-30 14:44:13.941信息1007---[main]o.a.c.m.ManagedManagement策略:JMX已启用
2019-04-30 14:44:14.120信息1007---[main].c.i.InterceptSendToMockEndpointStrategy:Adviced endpoint[http://help.co.za:10108]使用模拟端点[mock:http:help.co.za:10108]
2019-04-30 14:44:14.125信息1007---[main].c.i.InterceptSendToMockEndpointStrategy:Adviced端点[direct://processResponse]使用模拟端点[mock:direct:processResponse]
2019-04-30 14:44:14.151信息1007---[main].c.i.InterceptSendToMockEndpointStrategy:Adviced端点[jms://queue.sample?exchangePattern=InOut]使用模拟端点[mock:jms:queue.sample]
2019-04-30 14:44:14.165信息1007---[main]o.a.camel.spring.SpringCamelContext:未使用StreamCaching。如果使用流,则建议启用流缓存。有关更多详细信息,请参阅http://camel.apache.org/stream-caching.html
2019-04-30 14:44:14.259信息1007---[main]o.a.camel.spring.SpringCamelContext:Route:Build-Send-Http请求已启动并从以下位置使用:direct://start
2019-04-30 14:44:14.259信息1007---[main]o.a.camel.spring.SpringCamelContext:Route:Build-XML响应启动并从以下位置使用:direct://processResponse
2019-04-30 14:44:14.291信息1007---[main]o.a.camel.spring.SpringCamelContext:Route:Receive JMS消息已启动并从以下位置使用:jms://queue.sample?exchangePattern=InOut
2019-04-30 14:44:14.293信息1007---[main]o.a.camel.spring.SpringCamelContext:Route:route1从以下地点开始使用:direct://jmsMessage
2019-04-30 14:44:14.294信息1007---[main]o.a.camel.spring.SpringCamelContext:Route:route2从以下位置开始和消费:direct://jsonResponse
2019-04-30 14:44:14.294信息1007---[main]o.a.camel.spring.SpringCamelContext:总共5条路线,其中5条已经开通
2019-04-30 14:44:14.295信息1007---[main]o.a.camel.spring.SpringCamelContext:apachecamel 2.23.2(CamelContext:camel-1)在0.355秒内启动
2019-04-30 14:44:14.478信息1007---[main]c.i.o.r.OP_GetTypes.TypeServiceRouter:org.apache.camel.http.common.HttpOperationFailedException:http操作调用失败http://help.co.za:10108/ 状态代码:404
位于org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:273)```
尝试将参数更改为interceptSendToEndpoint,如下所示
interceptSendToEndpoint("http://.*")
看起来正则表达式与http端点不匹配,因为chracter
*
本身不匹配。但是*
匹配http://
之后的所有字符,请尝试将参数更改为interceptSendToEndpoint,如下所示
interceptSendToEndpoint("http://.*")
看起来正则表达式与http端点不匹配,因为chracter
*
本身不匹配。但是*
匹配http://
您使用什么版本的Camel,以及:{{http.endpoints.GetTypes}的值是多少?你可以从信息日志中看到什么被camel.Im使用了camel的2.20.1版。{{http.endpoints.GetTypes}}的值是:“http://help.co.za:10108/grc/api/types/72?includeFieldDefinitions=false&includeLocalizedLabels=false". 我甚至尝试使用一个通配符:http://*日志显示:2019-04-30 11:55:39.059 INFO 13951---[main]org.apache.camel.model.routedDefinition:advice with route after:route(Build&Send http Request)[[From[direct:start]]->[OnException[[class java.lang.Exception]->[Log[${Exception.stacktrace}],Bean[cba.integrations.opad.processors.getTypes。GetTypesProcess@37854b34]]],InterceptSendToEndpoint[http://*->[SetBody[“”]嗯,你可以尝试升级Camel,看看它是否能正常工作。上面的日志表明路线已经通知。你也可以尝试使用http*,因为wildicardi已经将我的Camel更新为2.23.2,但仍然没有成功。我已经为这个问题添加了一个更详细的日志,希望它能指出我将在哪里出错Camel d的哪个版本o您使用,并且:{{http.endpoints.GetTypes}}的值是什么?您可以从信息日志中看到什么被camel.Im使用了camel的2.20.1版。值:{{http.endpoints.GetTypes}是:“http://help.co.za:10108/grc/api/types/72?includeFieldDefinitions=false&includeLocalizedLabels=false"我甚至尝试使用一个通配符:http://*日志显示:2019-04-30 11:55:39.059 INFO 13951---[main]org.apache.camel.model.routedDefinition:advice with route after:route(Build&Send http Request)[[From[direct:start]]->[OnException[[class java.lang.Exception]->[Log[${Exception.stacktrace}],Bean[cba.integrations.opad.processors.getTypes。GetTypesProcess@37854b34]]],InterceptSendToEndpoint[http://*
interceptSendToEndpoint("http://.*")