我目前正在尝试新的 REST DSL Apache Camel 2.14.0 版本。正如这篇文章的标题所述,我与 String bean 发生了冲突。让我们看看哪里出了问题。
这是一个简化为测试用例的有效 XML 文件。它只定义了一个字符串 bean 和一个包含休息端点和由休息端点调用的单个路由的 Camel 上下文。
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd"
>
<bean id="source-directory" class="java.lang.String">
<constructor-arg type="java.lang.String" value="file:/opt/a/directory/data/audio" />
</bean>
<camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
<dataFormats>
<json id="jack" library="Jackson" unmarshalTypeName="org.apache.camel.component.jackson.TestPojo"/>
</dataFormats>
<restConfiguration bindingMode="json" component="restlet" port="5117" />
<rest path="/rest-api/">
<get uri="/{words}/" consumes="application/json" produces="application/json">
<to uri="direct:words" />
</get>
</rest>
<route>
<from uri="direct:words" />
<transform>
<simple>${headers.words}</simple>
</transform>
</route>
</camelContext>
</beans>
为了加载和测试这个 Camel 上下文,我使用了以下测试用例:
import org.apache.camel.CamelContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Test {
@org.junit.Test
public void testT() throws Exception {
final FileSystemXmlApplicationContext bean = new FileSystemXmlApplicationContext("src/test/resources/camelContext.xml");
final CamelContext context = bean.getBean("camelContext", CamelContext.class);
context.start();
Thread.yield();
Thread.sleep(600000);
}
}
它当前导致以下错误:
org.apache.camel.RuntimeCamelException: org.apache.camel.FailedToCreateRouteException: Failed to create route route2 at: >>> RestBinding <<< in route: Route(route2)[[From[rest:get:/rest-api/:/{words}/?produces=a... because of Provider com.sun.xml.bind.v2.ContextFactory could not be instantiated: javax.xml.bind.JAXBException: "file" ne contient pas ObjectFactory.class ou jaxb.index
即"file"不包含 ObjectFactory.class 或 jaxb.index
似乎删除 source-directory
bean 声明或 rest 端点声明可以解决问题,因此它们之间似乎存在不兼容性;但是,作为 Camel 的新手,我无法弄清楚问题出在哪里。
有人可以给我线索吗?我做错了什么吗?
提前致谢, 亚瑟。
最佳答案
我认为这个问题的发生是由于您为单元测试加载 Spring 和 Camel 上下文所采用的机制。考虑改用 CamelTestSupport .
我使用这个范例重写了这个测试,一切正常。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/META-INF/spring/camel-context.xml" })
public class Test extends CamelTestSupport {
@org.junit.Test
public void testT() throws Exception {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpResponse response = httpClient.execute(new HttpGet(
"http://localhost:5117/rest-api/hello"));
assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals("\"hello\"", new BufferedReader(new InputStreamReader(response.getEntity()
.getContent(), StandardCharsets.UTF_8)).readLine());
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder()
{
@Override
public void configure() throws Exception {
from("direct:words").transform().simple("${headers.words}");
}
};
}
}
关于java - Camel - REST DSL (2.14.0) 和 String bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26840213/