使用Apache Camel表达REST服务的方法
作者:方石剑 发布时间:2023-08-27 03:41:56
使用Apache Camel的REST服务
Apache Camel可以作为一个独立的或嵌入的库在任何地方运行,它可以帮助整合。继续阅读,了解如何使用它来暴露REST服务。
如何使用Apache Camel来表达REST服务
Camel REST允许使用Restlet、Servlet和许多这样的HTTP感知组件来实现REST服务的创建。
大家都知道,Camel的主要功能是路由引擎。路由可以使用基于Java的DSL或基于XML来开发。在这篇文章中,我将按照JavaDSL来开发一个REST服务。
定义端点
为了定义端点,我们需要使用Apache Camel DSL与 Java DSL(尽管你可以使用XML)。
下面是Java DSL。
Java
rest("/api/products")
.get().route().to("...")
.post().route().to("...")
.delete().route().to("...");
它与Camel路由类似,但使用rest()
。我们需要提到用于暴露端点的组件服务。Camel支持以下组件来实现Bootstrap REST服务。
Servlet
Spark REST
Netty HTTP
Jetty
如果你打算将Camel与Spring Boot框架集成以暴露服务,最好使用servlet
组件,因为Spring Boot支持嵌入式Tomcat,Camel可以使用它。
让我们把REST配置成。
Java
// Define the implementing component - and accept the default host and port
restConfiguration()
.component("servlet");
如何覆盖端口
你可以用你选择的任何其他端口号来覆盖默认的8080端口,方法是将.port()
设置为restConfiguration()
API,或者,如果你将Apache Camel与Spring Boot集成,你可以使用application.properties
中的server.port=8082
。
覆盖上下文路径
默认情况下,Camel将导入请求映射到/camel/*
。你可以通过使用application.properties
作为camel.component.servlet.mapping.context-path=/services/api/*
,将其覆盖到你选择的任何特定路径。
配置绑定模式,将请求集合到POJO对象。如果设置为 "off "以外的任何内容,生产者将尝试把传入信息的主体从inType转换为JSON或XML,而把响应从JSON或XML转换为outType。有五个枚举,其值可以是以下之一:自动、关闭、JSON、XML或json_xml。为了实现这一点,你需要将绑定模式设置为restConfiguration()
,因为bindingMode(RestBindingMode.auto);
。
请看下面的REST API的配置样本。
@Component
public class HttpRouteBuilder extends BaseRouteBuilder {
@Override
public void configure() throws Exception {
super.configure();
// it tells Camel how to configure the REST service
restConfiguration()
// Use the 'servlet' component.
// This tells Camel to create and use a Servlet to 'host' the RESTful API.
// Since we're using Spring Boot, the default servlet container is Tomcat.
.component("servlet")
// Allow Camel to try to marshal/unmarshal between Java objects and JSON
.bindingMode(RestBindingMode.auto);
rest().get("/kyc/{uid}").route().process("httpRequestProcessor").to("log:?level=INFO&showBody=true").endRest();
rest().post("/kyc").type(RequestObject.class).route().to("bean-validator:myvalidatorname")
.process("httpRequestProcessor").to("log:?level=INFO&showBody=true");
}
}
您可以使用Apache Camel bean验证器组件验证传入的请求,这需要在您的Maven POM中添加camel-bean-validator
依赖关系。
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bean-validator</artifactId>
</dependency>
在请求对象中定义验证规则
为了实现输入请求验证,你需要为POJO/请求类中的字段添加验证注解。这些注释可在包javax.validation.constraints
。JSR-303 API中最常见的是。
@NotNull
- 检查该字段是否是null
@AssertTrue
/@AssertFalse
- 检查该字段是否为真或假@Pattern(regex=, flags=)
- 检查该字段是否与给定的 ,与给定的regex
flags
在org.hibernate.validator.constraints
,有一些Hibernate特有的注释,比如。
@Email
- 检查该字段是否包含一个有效的电子邮件地址@CreditCardNumber
- 这个可能很明显@NotEmpty
- 检查注解的字段是否为空或空。
如何处理异常
你可以处理不同类型的异常,并使用Apache Camel异常条款(onException
)向客户端发送自定义的错误信息,无论是在路由级别还是在全球级别。你也可以重写REST API调用的HTTP响应代码和消息。
public class BaseRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
onException(BeanValidationException.class).handled(true).process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
Throwable cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, 400);
exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, MediaType.APPLICATION_JSON);
exchange.getMessage().setBody("{error:" + cause.getMessage() + "}");
}
});
onException(InvalidRequestException.class).handled(true).process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
Throwable cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, 400);
exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, MediaType.APPLICATION_JSON);
exchange.getMessage().setBody("{error:" + cause.getMessage() + "}");
}
});
onException(Exception.class).handled(true).process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
Throwable cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, 500);
exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, MediaType.APPLICATION_JSON);
exchange.getMessage().setBody("{error:" + cause.getMessage() + "}");
}
});
}
注意:在这里我创建了一个基类来处理各种异常,在我的主REST API构建器类(HttpRouteBuilder
)中,它扩展了BaseRouteBuilder
。
最后是POM。
<dependencyManagement>
<dependencies>
<!-- Spring Boot BOM -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Camel BOM -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-dependencies</artifactId>
<version>${camel.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jackson-starter</artifactId>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-servlet-starter</artifactId>
</dependency>
<!-- Testing Dependencies -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-spring</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-swagger-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bean-validator</artifactId>
</dependency>
</dependencies>
总结
现在你知道了如何用Camel暴露REST API,你可能想知道什么时候/为什么要用Apache Camel来构建REST服务。简单的答案是,如果你已经在使用Apache Camel来整合不同协议和应用程序之间的数据,那么REST是你需要支持的另一个数据源,而不是用Spring Boot或任何其他框架来构建REST服务。你可以利用Camel REST组件来暴露REST API,并使用已知的Camel DSL来消费/生产消息,这有助于你规范技术桩。你还可以扩展Camel REST,使其包括Swagger,以便使用camel-swagger
组件提供API规范。
来源:https://juejin.cn/post/7107138393548521508


猜你喜欢
- 图为山寨央视网站首页。(记者 齐广君 摄)山寨网站冒充央视骗钱财 民警提醒市民要警惕“非常6+1”是
- 这可能是所有新站长都面临的一个问题,对于一个站点来说,做友情链接是非常不错的一种推广网站的手段,只是这个链接到底该怎么做,不少人都是非常盲目
- 一款“开心农场”让众多网民非网民们见识到了WebGame的力量到底有多大,那些起早摸黑半夜钻出被窝不惧寒冷只为偷菜的人们,在所有人看来这是极
- “如果不刷信用,谁会来买你的东西?”“当然选刷。”&
- 说微软的IE系列产品都被众多的前端开发者臭骂,除去其对于WEB标准化的支持问题外,再者是因为其同一款产品的存活型号太多,从IE6.0-IE8
- Godaddy用户如何使用File Manager删除一个目录呢?整理了详细的教程供您参考。1、 登陆你的Account Manager.
- 计算机可以被设置为每次重新启动时,都可以在两个或多个操作系统之间选择。例如,可以将服务器设置为大部分时间运行 Windows 2000 Se
- 以前帮客户做好网站之后就轻松了,基本上不用再管,如果有什么问题,也只是负责修改某些功能而已,基本上牵扯不到网站的运营推广,哪些都是他们的事情
- 北京时间6月19日上午消息,据Mozilla基金会统计,Firefox 3正式推出后24小时内下载次数已经超过800万,这也是自Firefo
- 付款流程中有两个收入点需要注意,一个是 10 美元,一个是 100 美元。一.10美元,确认帐户信息当你的收入达到10美元时,系统会自动向你
- 最近在调研关联数据的一些东西,需要用到rdf数据库,所以接触了virtuoso数据库。安装的坑其实并不多,之前在windows 10上安过一
- 同Web站点一样,用户也可以使用多种方法来管理FTP站点服务器。例如:设置FTP站点服务器的虚拟目录的访问权限、设置匿名访问、创建用户账户等
- 正常情况下,我们在启动Windows Server 2008系统的时候,该操作系统会自动将安装在对应主机中的所有硬件全部加载成功,这个加载过
- 技术对于站长发展中的作用一直存在争论,但现在残酷的竞争面前,仅仅依靠通用论坛程序或者文档管理系统建立千篇一律的个人站点已经毫无出路。个人站长
- 广东的今天在初冬的季节里也略有凉意,起风了我站在7楼的窗台上观望!望着马路上为生活奔波的人们.感觉到眼前很模糊,因为此时的心情无法用语言来形
- 一、安装要求硬件要求CPU支持虚拟化、4核;内存10G网卡要求:最好两块虚拟网卡 (实现冗余)。硬盘要求:最好两块,一块作为系统盘、一块作为
- UCenter Home是Comsenz公司发布的一款SNS建站系统,目前最新版本是1.5。在UCenter Home中全局动态,就是会在站
- 旷土年前写过一篇标题为《旷土:中国商业网址的成功经验分享 初期发展篇》的文章,写得比较粗糙,今天就来详细谈谈运作中的一些经验。旷土在运作中国
- 互联网是当前非常热门的产业。网络营销已在同一时间成为一个高度熟练的和有竞争力的领域。作为一个具有超前意识的企业,会毫不犹豫的选择网络营销或搜
- V5MALL全线免费,再推网上商城炙不久之前相续在上海豫园商城、青岛特色街取得辉煌战果的上海威博网络技术有限公司,再其V5SHOP网店系统开