Apache Camel + Spring (war) + Tomcat + REST

限于喜欢 提交于 2019-12-20 07:06:34

问题


I am trying to develp a rest service using apache camel. My project is a spring mvc war deployed on tomcat.

I dont want to use apache cxf (cxf servlet).

        public class SampleRouter extends RouteBuilder {

            @override
            public void configure() throws Exception {
                from("cxfrs://http://localhost:1234/sample")
                .process (new Processor() {
                    public void process(Exchange exchange) throws Exception {
                        System.out.println("test");
                    }
                })).setBody(constant("SUCCESS"));
            }
        }


        @Path("/sample")
        public class SampleResource {

            @GET
            public void test() {

            }
        }

web.xml has dispatcherservlet, contextloaderlistener.

dispatcher-servlet.xml has mvc:annotation-drivem, context:component-scan,

        <camelContext id="server" trace="true" xmlns="http://camel.apache.org/schema/spring">
            <contextScan />
        </camelContext>

pom.xml has camel-core, camel-cxf, camel-stream, cxf-rt-transports-http-jetty, cxf-rs-frontend-jaxrs, camel-spring, spring-webmvc, spring-web, spring-context.

Tomcat runs on 8080, there seems to be no exception when server comes up. But, I tried hitting the url (http://localhost:1234/sample), nothing seems to be happening.

What am i missing? I would eventually extend this to REST to Spring DSL or REST to Java DSL with authentication, filters and interceptors.

I also tried cxf:rsServer and referred that in router class.

Also, in the future if i have to use https instead of http? or how do i have the url not hard-coded?


回答1:


It may be too late, but to consume HTTP requests, one may use Apache Camel Servlet component

http://camel.apache.org/servlet.html




回答2:


You need to setup the resourceClass option on the cxfrs endpoint. Here is an example

from("cxfrs://http://localhost:1234/sample?resourceClasses=my.pachage.SampleResource")

You can find some example in camel-cxfrs component page.

If you want to export a CXF service through servlet transport, you need to do some work as it said.

If you want to change the address dynamically, you can take look at the camel properties component.




回答3:


If you are looking to start a camel route by a consuming cxf rest service which uses the servlet transport then you need to :

  1. Clean up your pom.xml and remove any references to jetty.
  2. Add the CXF servlet to your web.xml

    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <!-- all our webservices are mapped under this URI pattern -->
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
    
  3. Add the servlet-transport dependency:

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>${cxf-version}</version>
     </dependency>
    
  4. In your spring/camel configuration

    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    
    <cxf:rsServer id="rsServer" address="/restService"
     serviceClass="com.something.test.SimpleServiceImpl"
     loggingFeatureEnabled="true" loggingSizeLimit="20" />
    
  5. Build a route from this consumer endpoint as:

    from("cxfrs:bean:rsServer?bindingStyle=SimpleConsumer")
        .to("log:TEST?showAll=true")
    
  6. You can now view/(invoke with a method) the endpoint using : http://host:port/context/services/restService?_wadl



来源:https://stackoverflow.com/questions/22188478/apache-camel-spring-war-tomcat-rest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!