Camel - REST DSL(2.14.0)和String bean

时间:2014-11-10 09:12:41

标签: java spring rest apache-camel

我目前正在尝试Apache Camel 2.14.0版本的新REST DSL。正如这篇文章的标题所述,我与String bean发生了冲突。让我们来看看有什么不对。

这是一个简化为测试用例的有效XML文件。它只定义了一个String 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

即#34;文件&#34;不包含ObjectFactory.class或jaxb.in​​dex

似乎删除source-directory bean声明或其余端点声明解决了问题,因此它们之间似乎存在不兼容性;但是,作为一个骆驼新手,我无法弄清楚问题是什么。

有人可以给我提供线索吗?我做错了吗?

提前致谢, 亚瑟。

2 个答案:

答案 0 :(得分:1)

我认为这个问题正在发生,因为您正在为您的单元测试加载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}");
            }
        };
    }
}

答案 1 :(得分:0)

感谢队友,我终于理解了错误。

问题来自&#34; java.lang.String&#34;骆驼语境中的豆子。 放

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

在驼峰语境中,我得到了以下日志:

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

我对情况的理解是,如果定义了任何java.lang.String对象,则JAXBContext将其用作工厂标识符。他们可能会寻找通常始终为@Autowired String的{​​{1}}属性。 取代&#34;来源&#34; bean将bean封装在属性中来解决问题。

发现意外的副作用!