【问题标题】:Camel - REST DSL (2.14.0) and String beanCamel - REST DSL (2.14.0) 和 String bean
【发布时间】:2014-11-10 09:12:40
【问题描述】:

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

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

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

谁能给我一些线索?我做错了吗?

提前致谢, 亚瑟。

【问题讨论】:

    标签: java spring rest apache-camel


    【解决方案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}");
                }
            };
        }
    }
    

    【讨论】:

    • 感谢您的工作!它没有解决我的问题,但我学到了很多关于骆驼测试的使用。我还向 Camel 邮件列表提出了这个问题,他们认为问题来自 jaxb 版本冲突。
    【解决方案2】:

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

    问题来自骆驼上下文中“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>
    

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

    [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 可以解决问题。

    发现了意外的副作用!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      相关资源
      最近更新 更多