为什么我用 camel servlet 创建的 REST api 没有公开?
Why my REST api created with camel servlet doesn't expose?
我目前正在努力使用 Apache Camel 在我的项目中公开 REST api。当我 运行 项目时,它在控制台中似乎没问题,但事实并非如此
工作:
curl: (7) Failed to connect to 127.0.0.1 port 8080: Connection refused
这是 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] DEBUG org.apache.camel.component.jms.JmsConsumer - Started listener container org.apache.camel.component.jms.DefaultJmsMessageListenerContainer@52045dbe on destination MATERIALS_ENRICHED
12:47:22.591 [main] DEBUG org.apache.camel.impl.engine.InternalRouteStartupManager - Route: route1 started and consuming from: activemq://topic:MATERIALS_ENRICHED
12:47:22.593 [main] DEBUG org.apache.camel.impl.engine.InternalRouteStartupManager - Route: route2 >>> Route[rest://get:/materials?consumerComponentName=servlet&routeId=route2 -> null]
12:47:22.593 [main] DEBUG org.apache.camel.impl.engine.InternalRouteStartupManager - Starting consumer (order: 1001) on route: route2
12:47:22.593 [main] DEBUG org.apache.camel.support.DefaultConsumer - Build consumer: Consumer[servlet:/materials?httpMethodRestrict=GET]
12:47:22.593 [main] DEBUG org.apache.camel.support.DefaultConsumer - Init consumer: Consumer[servlet:/materials?httpMethodRestrict=GET]
12:47:22.593 [main] DEBUG org.apache.camel.support.DefaultConsumer - Starting consumer: Consumer[servlet:/materials?httpMethodRestrict=GET]
12:47:22.595 [main] DEBUG org.apache.camel.http.common.DefaultHttpRegistry - Registering consumer for path /materials providers present: 0
12:47:22.595 [main] DEBUG org.apache.camel.impl.engine.InternalRouteStartupManager - Route: route2 started and consuming from: servlet:/materials
12:47:22.597 [main] INFO org.apache.camel.impl.engine.AbstractCamelContext - Routes startup summary (total:2 started:2)
12:47:22.597 [main] INFO org.apache.camel.impl.engine.AbstractCamelContext - Started route1 (activemq://topic:MATERIALS_ENRICHED)
12:47:22.597 [main] INFO org.apache.camel.impl.engine.AbstractCamelContext - Started route2 (rest://get:/materials)
12:47:22.597 [main] INFO org.apache.camel.impl.engine.AbstractCamelContext - Apache Camel 3.12.0 (camel-1) started in 525ms (build:81ms init:296ms start:148ms)
12:47:22.610 [main] DEBUG org.apache.camel.impl.DefaultCamelContext - start() took 457 millis
请在评论中告诉我我是否应该放置完整的构建输出以便我编辑票证。
请帮我。谢谢。
组件 camel-servlet
并不像您尝试的那样在独立模式下使用。当您在 Servlet 容器或应用程序服务器上部署 Camel 应用程序时,将使用它。
在独立模式下,如果你想公开一个休息端点,你应该使用像 camel-undertow
、camel-jetty
、camel-netty-http
或 camel-platform-http
这样的组件(你可以找到完整列表 here).
假设您要使用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);
我目前正在努力使用 Apache Camel 在我的项目中公开 REST api。当我 运行 项目时,它在控制台中似乎没问题,但事实并非如此 工作:
curl: (7) Failed to connect to 127.0.0.1 port 8080: Connection refused
这是 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] DEBUG org.apache.camel.component.jms.JmsConsumer - Started listener container org.apache.camel.component.jms.DefaultJmsMessageListenerContainer@52045dbe on destination MATERIALS_ENRICHED 12:47:22.591 [main] DEBUG org.apache.camel.impl.engine.InternalRouteStartupManager - Route: route1 started and consuming from: activemq://topic:MATERIALS_ENRICHED 12:47:22.593 [main] DEBUG org.apache.camel.impl.engine.InternalRouteStartupManager - Route: route2 >>> Route[rest://get:/materials?consumerComponentName=servlet&routeId=route2 -> null] 12:47:22.593 [main] DEBUG org.apache.camel.impl.engine.InternalRouteStartupManager - Starting consumer (order: 1001) on route: route2 12:47:22.593 [main] DEBUG org.apache.camel.support.DefaultConsumer - Build consumer: Consumer[servlet:/materials?httpMethodRestrict=GET] 12:47:22.593 [main] DEBUG org.apache.camel.support.DefaultConsumer - Init consumer: Consumer[servlet:/materials?httpMethodRestrict=GET] 12:47:22.593 [main] DEBUG org.apache.camel.support.DefaultConsumer - Starting consumer: Consumer[servlet:/materials?httpMethodRestrict=GET] 12:47:22.595 [main] DEBUG org.apache.camel.http.common.DefaultHttpRegistry - Registering consumer for path /materials providers present: 0 12:47:22.595 [main] DEBUG org.apache.camel.impl.engine.InternalRouteStartupManager - Route: route2 started and consuming from: servlet:/materials 12:47:22.597 [main] INFO org.apache.camel.impl.engine.AbstractCamelContext - Routes startup summary (total:2 started:2) 12:47:22.597 [main] INFO org.apache.camel.impl.engine.AbstractCamelContext - Started route1 (activemq://topic:MATERIALS_ENRICHED) 12:47:22.597 [main] INFO org.apache.camel.impl.engine.AbstractCamelContext - Started route2 (rest://get:/materials) 12:47:22.597 [main] INFO org.apache.camel.impl.engine.AbstractCamelContext - Apache Camel 3.12.0 (camel-1) started in 525ms (build:81ms init:296ms start:148ms) 12:47:22.610 [main] DEBUG org.apache.camel.impl.DefaultCamelContext - start() took 457 millis
请在评论中告诉我我是否应该放置完整的构建输出以便我编辑票证。 请帮我。谢谢。
组件 camel-servlet
并不像您尝试的那样在独立模式下使用。当您在 Servlet 容器或应用程序服务器上部署 Camel 应用程序时,将使用它。
在独立模式下,如果你想公开一个休息端点,你应该使用像 camel-undertow
、camel-jetty
、camel-netty-http
或 camel-platform-http
这样的组件(你可以找到完整列表 here).
假设您要使用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);