Camel REST DSL是否与Restlet Servlet一起使用?

时间:2015-01-05 20:33:27

标签: java servlets apache-camel restlet

我最近升级到Camel 2.14.1并且一直在玩新的REST DSL。在升级之前,我在servlet容器中使用了restlet,即在我的web.xml中使用它:

<!-- Restlet Servlet -->
<servlet>
  <servlet-name>RestletServlet</servlet-name>
  <servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class>
  <init-param>
    <param-name>org.restlet.component</param-name>
    <param-value>RestletComponent</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>RestletServlet</servlet-name>
  <url-pattern>/rs/*</url-pattern>
</servlet-mapping>

这在我的骆驼语境中:

<bean id="RestletComponent" class="org.restlet.Component" />

<bean id="RestletComponentService" class="org.apache.camel.component.restlet.RestletComponent">
  <constructor-arg index="0">
    <ref bean="RestletComponent" />
  </constructor-arg>
</bean>

这不适用于REST DSL。

我用这条简单的路线测试它:

<rest>
    <get uri="/hello">
        <to uri="direct:hello"/>
    </get>
</rest>
<route id="hello">
    <from uri="direct:hello"/>
    <setBody><constant>Dolly</constant></setBody>
</route>

REST DSL成功找到我的web.xml中定义的RestletComponent Bean,但bean没有与之关联的camelContext,因此当代码尝试访问上下文时,我得到一个空指针异常。

基本上,我开始怀疑REST DSL与servlet容器中的Restlet不兼容。我希望托管servlet容器处理传入的请求,我不想为我的驼峰上下文生成单独的restlet服务器进程(在新端口上)。

我运气不好吗?


好的,为了方便起见,我从一个现有的例子开始:camel-example-restlet-jdbc,它使用了restlet并对其进行了修改,因此它使用了新的rest dsl。

这是xml-dsl.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">

<import resource="common.xml" />

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <rest>
        <post uri="/persons">
            <route>
                <setBody>
                    <simple>insert into person(firstName, lastName) values('${header.firstName}','${header.lastName}')
                    </simple>
                </setBody>
                <to uri="jdbc:dataSource"/>

                <setBody>
                    <!--<simple>select * from person ORDER BY id desc OFFSET 1 ROWS</simple>-->
                    <simple>select * from person where id in (select max(id) from person)</simple>
                </setBody>
                <to uri="jdbc:dataSource"/>
            </route>
        </post>
        <get uri="/persons/{personId}">
            <route>
                <setBody>
                    <simple>select * from person where id = ${header.personId}</simple>
                </setBody>
                <to uri="jdbc:dataSource"/>
            </route>
        </get>
        <get uri="/persons">
            <route>
                <setBody>
                    <constant>select * from person</constant>
                </setBody>
                <to uri="jdbc:dataSource"/>
            </route>
        </get>
    </rest>

</camelContext>
</beans>

这不起作用。它抛出java.net.SocketException:Permission denied

1 个答案:

答案 0 :(得分:0)

之前我没有使用过Rest DSL,但根据documentation你可以明确地告诉Camel你正在使用restlet组件:

<restConfiguration component="RestletComponent" port="9091"> <componentProperty key="foo" value="123"/> </restConfiguration>

它确实说它会查找以检查是否有任何组件与DSL集成,如果没有指定,但我猜,但它值得给它一个镜头。

在旁注中,我发现它是bit odd,你以大写字母开头给你的春豆ID。