使用Apache?Camel表達REST服務的方法
使用Apache Camel的REST服務
Apache Camel可以作為一個獨立的或嵌入的庫在任何地方運行,它可以幫助整合。繼續(xù)閱讀,了解如何使用它來暴露REST服務。
如何使用Apache Camel來表達REST服務
Camel REST允許使用Restlet、Servlet和許多這樣的HTTP感知組件來實現(xiàn)REST服務的創(chuàng)建。
大家都知道,Camel的主要功能是路由引擎。路由可以使用基于Java的DSL或基于XML來開發(fā)。在這篇文章中,我將按照JavaDSL來開發(fā)一個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支持以下組件來實現(xiàn)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 "以外的任何內(nèi)容,生產(chǎn)者將嘗試把傳入信息的主體從inType轉換為JSON或XML,而把響應從JSON或XML轉換為outType。有五個枚舉,其值可以是以下之一:自動、關閉、JSON、XML或json_xml。為了實現(xiàn)這一點,你需要將綁定模式設置為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>
在請求對象中定義驗證規(guī)則
為了實現(xiàn)輸入請求驗證,你需要為POJO/請求類中的字段添加驗證注解。這些注釋可在包javax.validation.constraints 。JSR-303 API中最常見的是。
@NotNull- 檢查該字段是否是null@AssertTrue/@AssertFalse- 檢查該字段是否為真或假@Pattern(regex=, flags=)- 檢查該字段是否與給定的 ,與給定的regexflags
在org.hibernate.validator.constraints ,有一些Hibernate特有的注釋,比如。
@Email- 檢查該字段是否包含一個有效的電子郵件地址@CreditCardNumber- 這個可能很明顯@NotEmpty- 檢查注解的字段是否為空或空。
如何處理異常
你可以處理不同類型的異常,并使用Apache Camel異常條款(onException )向客戶端發(fā)送自定義的錯誤信息,無論是在路由級別還是在全球級別。你也可以重寫REST API調(diào)用的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() + "}");
}
});
}注意:在這里我創(chuàng)建了一個基類來處理各種異常,在我的主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>總結
現(xiàn)在你知道了如何用Camel暴露REST API,你可能想知道什么時候/為什么要用Apache Camel來構建REST服務。簡單的答案是,如果你已經(jīng)在使用Apache Camel來整合不同協(xié)議和應用程序之間的數(shù)據(jù),那么REST是你需要支持的另一個數(shù)據(jù)源,而不是用Spring Boot或任何其他框架來構建REST服務。你可以利用Camel REST組件來暴露REST API,并使用已知的Camel DSL來消費/生產(chǎn)消息,這有助于你規(guī)范技術樁。你還可以擴展Camel REST,使其包括Swagger,以便使用camel-swagger 組件提供API規(guī)范。
到此這篇關于使用Apache Camel表達REST服務的方法的文章就介紹到這了,更多相關Apache Camel的REST服務內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
PHP程序員玩轉Linux系列 使用supervisor實現(xiàn)守護進程
這篇文章主要為大家詳細介紹了PHP程序員玩轉Linux系列文章,使用supervisor實現(xiàn)守護進程,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04

