java - Camel - REST DSL (2.14.0) 和 String bean

标签 java spring rest apache-camel

我目前正在尝试新的 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.in​​dex

似乎删除 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/

相关文章:

java - 无法解决在 Java 中使用 Camel CDI 在 Wildfly 中执行 Grrovy 脚本的类问题

java - 如何更新 SQLite 数据库(登录和注册系统)中的密码?

java - Spring MVC 应用程序中的 404 错误

java - 如何让 RestTemplate 使用 UriComponents 和 EncodingMode.VALUES_ONLY 对所有字符进行编码?

java - 我怎样才能得到 "href"里面的特定部分

javascript - Spring 和 Angular - 非常简单的 REST 请求

java - Spring Security应用程序调用事件监听器

spring - 使用 jersey 2 和 Spring 编写 "clean"集成测试 - REST

java - 如何使用 Retrofit 和 GSON 解析 [] 包围的 JSON 对象列表?

java - ITestListener - ExtentReport