我目前正在努力使用ApacheCamel在我的项目中公开RESTapi。当我运行该项目时,它在控制台中似乎很好,但它就是不好工作:
curlhttp://127.0.0.1:8080/materials
curl:(7(无法连接到127.0.0.1端口8080:连接拒绝
以下是pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Packages</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jdbc</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jsonpath</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId >
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.16.2</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-camel</artifactId>
<version>5.16.4</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jaxb</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jaxb-starter</artifactId>
<version>2.25.4</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jms</artifactId>
<version>3.15.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
<version>5.16.4</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.4.0-b180830.0359</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-xml-provider</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-rest</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-servlet</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-servlet-starter</artifactId>
<version>3.0.0-RC3</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson-starter</artifactId>
<version>3.0.0-RC3</version>
</dependency>
</dependencies>
</project>
这是我写的代码:
package com.release11.output;
import com.release11.xjc.materials.ObjectFactory;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.converter.jaxb.JaxbDataFormat;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.rest.RestBindingMode;
import javax.jms.Connection;
import javax.xml.bind.JAXBContext;
public class OutputAdapter1 {
public static void main(String[] args) throws Exception{
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
CamelContext context = new DefaultCamelContext();
context.addComponent("activemq", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
JaxbDataFormat xmlDataFormat = new JaxbDataFormat(jaxbContext);
xmlDataFormat.setIgnoreJAXBElement(false);
restConfiguration()
.component("servlet")
.host("localhost")
.port("8080")
.bindingMode(RestBindingMode.auto);
from("activemq:topic:MATERIALS_ENRICHED")
.unmarshal(xmlDataFormat)
.filter(simple("${body.type} in 'A1,A2,A3'"))
.to("direct:materials");
rest("/materials")
.get().route()
.to("direct:materials");
}
});
context.start();
}
}
我不确定是否将构建输出放在这里,因为它很长,所以我只将一个片段放在这些路由开始的地方:
12:47:22.591〔main〕调试org.apache.camel.component.jms.JmsConsumer-已启动侦听器容器org.apache.camel.component.jms.DefaultJmsMessageListenerContainer@52045dbe在目的地MATERIALS_ENRICHED12:47:22.591[主]调试org.apache.camel.impl.engine.InteralRouteStartupManager-路由:路由1启动并消耗自:activemq://topic:MATERIALS_ENRICHED12:47:22.593[主]调试org.apache.camel.impl.engine.InteralRouteStartupManager-路由:route2>gt>路线[rest://get:/materials?consumerComponentName=servlet&routeId=route2->空]12:47:22.593[主]调试org.apache.camel.impl.engine.InteralRouteStartupManager-正在路由上启动消费者(订单:1001(:route212:47:22.593[main]调试org.apache.camel.support.DefaultConsumer-生成消费者:消费者[servlet:/materials?httpMethodRestrict=GET]12:47:22.593[main]调试org.apache.camel.support.DefaultConsumer-初始化消费者:消费者[servlet:/materials?httpMethodRestrict=GET]12:47:22.593[main]调试org.apache.camel.support.DefaultConsumer-启动使用者:consumer[servlet:/materials?httpMethodRestrict=GET]12:47:22.595[main]调试org.apache.camel.http.common.DefaultHttpRegistry-正在注册路径/材料提供者的使用者:012:47:22.595[主]调试org.apache.camel.impl.engine.InteralRouteStartupManager-路由:路由2启动并消耗自:servlet:/materials12:47:22.597[main]INFO org.apache.camel.impl.engine.AbstractCamelContext-路由启动摘要(总共:2个启动:2个(12:47:22.597[min]INFO org.apache.camel.impl.engine.AbstractCamelContext-已启动路线1(activemq://topic:MATERIALS_ENRICHED)12:47:22.597[min]INFO org.apache.camel.impl.engine.AbstractCamelContext-已启动路线2(rest://get:/materials)12:47:22.597[main]INFO org.apache.camel.impl.engine.AbstractCamelContext-apache camel 3.12.0(camel-1(在525ms内启动(内部版本:81ms初始化:296ms启动:148ms(12:47:22.610[main]调试org.apache.camel.impl.DefaultCamelContext-start((占用457毫秒
请在评论中通知我,我是否应该输入完整的构建输出,以便编辑票证。请帮帮我。谢谢。
组件camel-servlet
不打算像您尝试的那样在独立模式下使用。它应该在Servlet容器或应用程序服务器上部署Camel应用程序时使用。
在独立模式中,如果要公开rest端点,则应该使用类似camel-undertow
、camel-jetty
、camel-netty-http
或camel-platform-http
的组件(可以在此处找到完整列表(。
假设您想使用undertow
,则必须遵循的下一步操作
在您的pom文件中,您需要将camel-rest
替换为camel-undertow
,如下所示:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-undertow</artifactId>
<version>3.12.0</version>
</dependency>
然后,您只需要更改休息配置即可使用undertow
,这应该很好:
restConfiguration()
.component("undertow")
.port(8080)
.bindingMode(RestBindingMode.auto);