简体   繁体   中英

Does Camel REST DSL work with a Restlet Servlet?

I recently upgraded to Camel 2.14.1 and have been playing around the new REST DSL. Before the upgrade I used restlet within a servlet container, ie with this in my 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>

And this in my camel context:

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

This does not work with the REST DSL.

I'm testing it out with this simple route:

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

The REST DSL successfully finds the RestletComponent Bean defined in my web.xml, but the bean does not have a camelContext associated with it, so I get a null pointer exception when the code tries to access the context.

Basically, I'm beginning to suspect that the REST DSL is incompatible with Restlet within a servlet container. I want the hosting servlet container to handle the incoming requests, I don't want to have to spawn a separate restlet server process (on a new port) for my camel contexts.

Am I out of luck?


OK, to make things easier, I started from one of the existing examples: camel-example-restlet-jdbc which uses restlet and altered it so it uses the new rest dsl.

Here's the xml-dsl.xml file:

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

This doesn't work. It throws java.net.SocketException: Permission denied

I haven't used the Rest DSL before, but according to the documentation you can explicitly let Camel know that you are using the restlet component:

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

It does say that it will lookup to check whether there are any components that integrates with the DSL if this is not specified, but I guess but it's worth giving it a shot.

On a side note, I found it a bit odd that you give your spring beans IDs starting with an uppercase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM