Camel - REST DSL (2.14.0) and String bean



我目前正在尝试Apache Camel 2.14.0版本的新REST DSL。正如这篇文章的标题所说,我与Stringbean发生了冲突。让我们看看哪里出了问题。

以下是一个简化为测试用例的有效XML文件。它只定义了一个Stringbean和一个Camel上下文,其中包含一个rest端点和由rest端点调用的单个路由。

<?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

也就是说,"文件"不包含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}");
            }
        };
    }
}

多亏了一位队友,我终于明白了这个错误。

问题来自camel上下文中"java.lang.String"bean的定义。放置时

<bean id="source" class="java.lang.String">
    <constructor-arg type="java.lang.String" value="file:/opt/a/directory/data/audio" />
</bean>

在camel上下文中,我得到了以下日志:

[main] JaxbDataFormat INFO Creating JAXBContext with contextPath: file:/opt/a/directory/data/audio and ApplicationContextClassLoader: sun.misc.Launcher$AppClassLoader@3485def8

我对这种情况的理解是,如果定义了任何java.lang.String对象,那么它就会被JAXBContext用作工厂标识符。他们可能会寻找一个通常总是null@Autowired String属性。将"源"bean替换为将我的sting封装在属性中的bean可以解决问题。

发现意外副作用!

最新更新