使用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=)
- 檢查該字段是否與給定的 ,與給定的regex
flags
在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寫(xiě)shell需要注意的問(wèn)題(必看)
下面小編就為大家?guī)?lái)一篇linux寫(xiě)shell需要注意的問(wèn)題(必看)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12PHP程序員玩轉(zhuǎn)Linux系列 使用supervisor實(shí)現(xiàn)守護(hù)進(jìn)程
這篇文章主要為大家詳細(xì)介紹了PHP程序員玩轉(zhuǎn)Linux系列文章,使用supervisor實(shí)現(xiàn)守護(hù)進(jìn)程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Linux系統(tǒng)網(wǎng)卡設(shè)置教程
這篇文章主要介紹了Linux系統(tǒng)網(wǎng)卡的設(shè)置教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Linux中g(shù)it用https連接時(shí)不用每次輸入密碼的方法
這篇文章主要給大家介紹了關(guān)于Linux中g(shù)it使用https連接時(shí)不用每次輸入密碼的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06Apache?Pulsar集群搭建部署詳細(xì)過(guò)程
這篇文章主要介紹了Apache?Pulsar集群搭建過(guò)程,搭建Pulsar集群至少需要3個(gè)組件:ZooKeeper集群、BookKeeper集群和Broker集群,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02