使用Apache?Camel表達(dá)REST服務(wù)的方法
使用Apache Camel的REST服務(wù)
Apache Camel可以作為一個(gè)獨(dú)立的或嵌入的庫(kù)在任何地方運(yùn)行,它可以幫助整合。繼續(xù)閱讀,了解如何使用它來(lái)暴露REST服務(wù)。
如何使用Apache Camel來(lái)表達(dá)REST服務(wù)
Camel REST允許使用Restlet、Servlet和許多這樣的HTTP感知組件來(lái)實(shí)現(xiàn)REST服務(wù)的創(chuàng)建。
大家都知道,Camel的主要功能是路由引擎。路由可以使用基于Java的DSL或基于XML來(lái)開(kāi)發(fā)。在這篇文章中,我將按照J(rèn)avaDSL來(lái)開(kāi)發(fā)一個(gè)REST服務(wù)。
定義端點(diǎn)
為了定義端點(diǎn),我們需要使用Apache Camel DSL與 Java DSL(盡管你可以使用XML)。
下面是Java DSL。
Java
rest("/api/products")
.get().route().to("...")
.post().route().to("...")
.delete().route().to("...");它與Camel路由類(lèi)似,但使用rest() 。我們需要提到用于暴露端點(diǎn)的組件服務(wù)。Camel支持以下組件來(lái)實(shí)現(xiàn)Bootstrap REST服務(wù)。
- Servlet
- Spark REST
- Netty HTTP
- Jetty
如果你打算將Camel與Spring Boot框架集成以暴露服務(wù),最好使用servlet 組件,因?yàn)镾pring Boot支持嵌入式Tomcat,Camel可以使用它。
讓我們把REST配置成。
Java
// Define the implementing component - and accept the default host and port
restConfiguration()
.component("servlet");如何覆蓋端口
你可以用你選擇的任何其他端口號(hào)來(lái)覆蓋默認(rèn)的8080端口,方法是將.port() 設(shè)置為restConfiguration() API,或者,如果你將Apache Camel與Spring Boot集成,你可以使用application.properties 中的server.port=8082 。
覆蓋上下文路徑
默認(rèn)情況下,Camel將導(dǎo)入請(qǐng)求映射到/camel/* 。你可以通過(guò)使用application.properties 作為camel.component.servlet.mapping.context-path=/services/api/*,將其覆蓋到你選擇的任何特定路徑。
配置綁定模式,將請(qǐng)求集合到POJO對(duì)象。如果設(shè)置為 "off "以外的任何內(nèi)容,生產(chǎn)者將嘗試把傳入信息的主體從inType轉(zhuǎn)換為JSON或XML,而把響應(yīng)從JSON或XML轉(zhuǎn)換為outType。有五個(gè)枚舉,其值可以是以下之一:自動(dòng)、關(guān)閉、JSON、XML或json_xml。為了實(shí)現(xiàn)這一點(diǎn),你需要將綁定模式設(shè)置為restConfiguration() ,因?yàn)?code>bindingMode(RestBindingMode.auto); 。
請(qǐng)看下面的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驗(yàn)證器組件驗(yàn)證傳入的請(qǐng)求,這需要在您的Maven POM中添加camel-bean-validator 依賴(lài)關(guān)系。
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-bean-validator</artifactId> </dependency>
在請(qǐng)求對(duì)象中定義驗(yàn)證規(guī)則
為了實(shí)現(xiàn)輸入請(qǐng)求驗(yàn)證,你需要為POJO/請(qǐng)求類(lèi)中的字段添加驗(yàn)證注解。這些注釋可在包javax.validation.constraints 。JSR-303 API中最常見(jiàn)的是。
@NotNull- 檢查該字段是否是null@AssertTrue/@AssertFalse- 檢查該字段是否為真或假@Pattern(regex=, flags=)- 檢查該字段是否與給定的 ,與給定的regexflags
在org.hibernate.validator.constraints ,有一些Hibernate特有的注釋?zhuān)热纭?/p>
@Email- 檢查該字段是否包含一個(gè)有效的電子郵件地址@CreditCardNumber- 這個(gè)可能很明顯@NotEmpty- 檢查注解的字段是否為空或空。
如何處理異常
你可以處理不同類(lèi)型的異常,并使用Apache Camel異常條款(onException )向客戶(hù)端發(fā)送自定義的錯(cuò)誤信息,無(wú)論是在路由級(jí)別還是在全球級(jí)別。你也可以重寫(xiě)REST API調(diào)用的HTTP響應(yīng)代碼和消息。
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() + "}");
}
});
}注意:在這里我創(chuàng)建了一個(gè)基類(lèi)來(lái)處理各種異常,在我的主REST API構(gòu)建器類(lèi)(HttpRouteBuilder)中,它擴(kuò)展了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>總結(jié)
現(xiàn)在你知道了如何用Camel暴露REST API,你可能想知道什么時(shí)候/為什么要用Apache Camel來(lái)構(gòu)建REST服務(wù)。簡(jiǎn)單的答案是,如果你已經(jīng)在使用Apache Camel來(lái)整合不同協(xié)議和應(yīng)用程序之間的數(shù)據(jù),那么REST是你需要支持的另一個(gè)數(shù)據(jù)源,而不是用Spring Boot或任何其他框架來(lái)構(gòu)建REST服務(wù)。你可以利用Camel REST組件來(lái)暴露REST API,并使用已知的Camel DSL來(lái)消費(fèi)/生產(chǎn)消息,這有助于你規(guī)范技術(shù)樁。你還可以擴(kuò)展Camel REST,使其包括Swagger,以便使用camel-swagger 組件提供API規(guī)范。
到此這篇關(guān)于使用Apache Camel表達(dá)REST服務(wù)的方法的文章就介紹到這了,更多相關(guān)Apache Camel的REST服務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux實(shí)現(xiàn)雙網(wǎng)卡綁定的代碼詳解
linux 主機(jī)安裝雙網(wǎng)卡,共享一個(gè)IP地址,對(duì)外提供訪(fǎng)問(wèn),實(shí)際同樣連接兩條物理線(xiàn)路到交換機(jī)實(shí)現(xiàn)平時(shí)雙網(wǎng)卡同時(shí)工作,分流網(wǎng)絡(luò)壓力,同時(shí)提供冗余備份,監(jiān)控,防止物理線(xiàn)路的單點(diǎn)故障,本文介紹了Linux實(shí)現(xiàn)雙網(wǎng)卡綁定的代碼示例,需要的朋友可以參考下2024-07-07
Linux運(yùn)維基礎(chǔ)進(jìn)程管理及環(huán)境組成分析
這篇文章主要為大家介紹了Linux運(yùn)維基礎(chǔ),對(duì)其中進(jìn)程管理及環(huán)境組成分析作了詳細(xì)的分析,有需要的朋友可以借鑒參考下,希望可以有所幫助2021-09-09
Linux使用fdisk進(jìn)行磁盤(pán)的相關(guān)操作
fdisk?命令是?Linux?中用于管理磁盤(pán)分區(qū)的強(qiáng)大文本實(shí)用程序,這篇文章主要為大家詳細(xì)介紹了如何使用fdisk進(jìn)行磁盤(pán)的相關(guān)操作,需要的可以了解下2025-01-01
詳解在LINUX上部署帶有JAR包的JAVA項(xiàng)目
這篇文章主要介紹了詳解在LINUX上部署帶有JAR包的JAVA項(xiàng)目,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03

