我正在使用Spring-jersey-camel创建一个应用程序。我想暴露我的泽西层并在内部调用camel路由来调用资源。
的web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
applicationContext.xml中
<camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
<packageScan>
<package>com.company.myapp.camel</package>
<excludes>**.*</excludes>
<includes>*Routes.java</includes>
</packageScan>
</camelContext>
MyRoutes.java
@Component
public final class MyRoutes extends RouteBuilder {
@Override
public void configure() throws Exception {
from("direct:getOrdersData").validate(body().isNotNull())
.log("Camel to get orders")
.to("restlet:http://localhost:8081/ordersapp/rest/order/123");
}
}
OrderResourceImpl.java
@Component
@Path("/orderLookup")
public class ReservationResources {
@org.apache.camel.produce
ProducerTemplate producer;
public void setProducer(ProducerTemplate producer) throws Exception {
this.producer = producer;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{orderId}")
public Response orderLookup(@PathParam("orderId") final long orderrId){
Response r = Response.noContent().build();
//Producer is null. throws nullPointerException
String order= producer.requestBody("direct:getOrdersData", orderId, String.class);
r = Response.ok().entity(reservation).build();
return r;
}
}
知道我做错了什么吗?或者如何在我的orderResourceImpl.java中注入myRoute / ProducerTemplate。提前致谢
答案 0 :(得分:3)
两个选项, 如果ReservationResources是一个spring bean,那么,将Camel Context注入其中并从中创建一个ProducerTemplate
ProducerTemplate template = camelContext.createProducerTemplate();
如果ReservationResources
不是spring bean,那么通过静态方法https://stackoverflow.com/a/13633109/3696510获取Camel Context,然后创建ProducerTemplate。
ProducerTemplate template = StaticSpringApplicationContext.getBean("camelContext").createProducerTemplate()
此外,如果您确实使用了链接中提到的StaticSpringApplicationContext
,我会将此方法添加到其中。
public static <T> T getBean(String beanName, Class<T> clazz) {
return (T) CONTEXT.getBean(beanName,clazz);
}