2

I am trying out a sample using swagger component of camel.

Below is my camel-context.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:camel="http://camel.apache.org/schema/spring"
    xmlns:cxf="http://camel.apache.org/schema/cxf"
    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">

    <!-- a bean for user services -->
    <bean id="StudentService"
        class="temp.org.impl.StudentService" />

    <camelContext id="mycamel" xmlns="http://camel.apache.org/schema/spring">

        <!-- configure rest to use the camel-servlet component, and use json binding 
            mode -->
        <!-- and tell to output json in pretty print mode -->
        <restConfiguration component="servlet" bindingMode="json"
            contextPath="school/bo" port="8080">
            <dataFormatProperty key="prettyPrint" value="true" />
        </restConfiguration>


        <!-- defines the rest services using the context-path /user -->
        <rest path="/students" consumes="application/json" produces="application/json">
            <description>Student service</description>

            <!-- this is a rest GET to view an user by the given id -->

            <get outType="temp.org.model.Student[]">
                <description>Get All Students</description>
                <responseMessage code="200" message="Student details" />
                <to uri="bean:StudentService?method=getStudents()" />
            </get>
            <post type="temp.org.model.Student"
                outType="temp.org.model.Student[]">
                <description>Query Student Object</description>
                <param name="body" type="body" description="Querying Student Object"
                    required="true" />
                <responseMessage code="404" message="Student not found" />
                <responseMessage code="200" message="Student details" />
                <route id="getStudentRoute">
                    <to uri="bean:StudentService?method=getStudent(${body})" />
                    <filter>
                        <simple>${body} == null</simple>
                        <setHeader headerName="Exchange.HTTP_RESPONSE_CODE">
                            <constant>404</constant>
                        </setHeader>
                    </filter>
                    <filter>
                        <simple>${body} != null</simple>
                        <setHeader headerName="Exchange.HTTP_RESPONSE_CODE">
                            <constant>200</constant>
                        </setHeader>
                    </filter>
                </route>
            </post>
            <delete type="temp.org.model.Student">
                <description>Delete alarm</description>
                <param name="body" type="body" description="Delete Student Object"
                    required="true" />
                <responseMessage code="404" message="Student not found" />
                <responseMessage code="200" message="Student deleted Successfully" />
                <to uri="bean:StudentService?method=deleteStudent(${body})" />
            </delete>
            <post type="temp.org.model.Student">
                <description>Insert alarm</description>
                <param name="body" type="body" description="Insert Student Object"
                    required="true" />
                <to uri="bean:StudentService?method=insertStudent(${body})" />
            </post>
        </rest>
    </camelContext>
</beans>

Now for this example i am using camel version 2.16 which has support for swagger. The problem is when i try to deploy the project in tomcat. While tomcat tries to read the camel-context.xml, it throws error as :

2016-03-08 15:57:00,736 [ost-startStop-1] ERROR ContextLoader                  - Context initialization failed
org.apache.camel.RuntimeCamelException: org.apache.camel.FailedToStartRouteException: Failed to start route route1 becau
se of Multiple consumers for the same endpoint is not allowed: Endpoint[servlet:/students?httpMethodRestrict=POST]
        at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1642)
        at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:138)
        at org.apache.camel.spring.CamelContextFactoryBean.onApplicationEvent(CamelContextFactoryBean.java:340)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMult
icaster.java:151)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMult
icaster.java:128)
        at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:3
31)
        at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:
775)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:434
)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:618)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:963)
        at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1600)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.camel.FailedToStartRouteException: Failed to start route route1 because of Multiple consumers for
the same endpoint is not allowed: Endpoint[servlet:/students?httpMethodRestrict=POST]
        at org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRouteConsumers(DefaultCamelContext.java:3486)
        at org.apache.camel.impl.DefaultCamelContext.doStartRouteConsumers(DefaultCamelContext.java:3462)
        at org.apache.camel.impl.DefaultCamelContext.safelyStartRouteServices(DefaultCamelContext.java:3392)
        at org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRoutes(DefaultCamelContext.java:3160)
        at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:3016)
        at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:175)
        at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:2812)
        at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:2808)
        at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:2831)
        at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:2808)
        at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
        at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:2777)
        at org.apache.camel.spring.SpringCamelContext.maybeStart(SpringCamelContext.java:270)
        at org.apache.camel.spring.SpringCamelContext.onApplicationEvent(SpringCamelContext.java:136)
        ... 22 more

Does camel-swagger support multiple post methods in a rest service? If yes can anybody provide me a sample to overcome the error i am getting..

Looking forward to your solutions. Thanks in advance..

Roy
  • 1,231
  • 1
  • 24
  • 61
  • 1
    How would you distinguish between which of the two `POST /students` operations should be called? – beny23 Mar 08 '16 at 13:02
  • Yes. I thought of the same. So regarding that I have a question as well that can we create unique post methods which uses body as request parameter – Roy Mar 09 '16 at 05:17
  • Different posts would have to be on different paths... – beny23 Mar 09 '16 at 05:24
  • Thanks for the reply beny. But can you give an sample example. – Roy Mar 09 '16 at 06:06

0 Answers0