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?
It may be too late, but to consume HTTP requests, one may use Apache Camel Servlet component
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.
If you are looking to start a camel route by a consuming cxf rest service which uses the servlet transport then you need to :
- Clean up your pom.xml and remove any references to jetty.
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>
Add the servlet-transport dependency:
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf-version}</version> </dependency>
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" />
Build a route from this consumer endpoint as:
from("cxfrs:bean:rsServer?bindingStyle=SimpleConsumer") .to("log:TEST?showAll=true")
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