人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

使用Apache?Camel表達(dá)REST服務(wù)的方法

發(fā)布日期:2022-07-15 19:30 | 文章來源:源碼之家

使用Apache Camel的REST服務(wù)

Apache Camel可以作為一個獨立的或嵌入的庫在任何地方運(yùn)行,它可以幫助整合。繼續(xù)閱讀,了解如何使用它來暴露REST服務(wù)。

如何使用Apache Camel來表達(dá)REST服務(wù)

Camel REST允許使用Restlet、Servlet和許多這樣的HTTP感知組件來實現(xiàn)REST服務(wù)的創(chuàng)建。

大家都知道,Camel的主要功能是路由引擎。路由可以使用基于Java的DSL或基于XML來開發(fā)。在這篇文章中,我將按照J(rèn)avaDSL來開發(fā)一個REST服務(wù)。

定義端點

為了定義端點,我們需要使用Apache Camel DSL與 Java DSL(盡管你可以使用XML)。

下面是Java DSL。

Java

rest("/api/products")
     .get().route().to("...")
     .post().route().to("...")
     .delete().route().to("...");

它與Camel路由類似,但使用rest() 。我們需要提到用于暴露端點的組件服務(wù)。Camel支持以下組件來實現(xiàn)Bootstrap REST服務(wù)。

  • Servlet
  • Spark REST
  • Netty HTTP
  • Jetty

如果你打算將Camel與Spring Boot框架集成以暴露服務(wù),最好使用servlet 組件,因為Spring Boot支持嵌入式Tomcat,Camel可以使用它。

讓我們把REST配置成。

Java

// Define the implementing component - and accept the default host and port
restConfiguration()
  .component("servlet");

如何覆蓋端口

你可以用你選擇的任何其他端口號來覆蓋默認(rèn)的8080端口,方法是將.port() 設(shè)置為restConfiguration() API,或者,如果你將Apache Camel與Spring Boot集成,你可以使用application.properties 中的server.port=8082 。

覆蓋上下文路徑

默認(rèn)情況下,Camel將導(dǎo)入請求映射到/camel/* 。你可以通過使用application.properties 作為camel.component.servlet.mapping.context-path=/services/api/*,將其覆蓋到你選擇的任何特定路徑。

配置綁定模式,將請求集合到POJO對象。如果設(shè)置為 "off "以外的任何內(nèi)容,生產(chǎn)者將嘗試把傳入信息的主體從inType轉(zhuǎn)換為JSON或XML,而把響應(yīng)從JSON或XML轉(zhuǎn)換為outType。有五個枚舉,其值可以是以下之一:自動、關(guān)閉、JSON、XML或json_xml。為了實現(xiàn)這一點,你需要將綁定模式設(shè)置為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 依賴關(guān)系。

<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=) - 檢查該字段是否與給定的 ,與給定的regex flags

org.hibernate.validator.constraints ,有一些Hibernate特有的注釋,比如。

  • @Email - 檢查該字段是否包含一個有效的電子郵件地址
  • @CreditCardNumber - 這個可能很明顯
  • @NotEmpty - 檢查注解的字段是否為空或空。

如何處理異常

你可以處理不同類型的異常,并使用Apache Camel異常條款(onException )向客戶端發(fā)送自定義的錯誤信息,無論是在路由級別還是在全球級別。你也可以重寫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)建了一個基類來處理各種異常,在我的主REST API構(gòu)建器類(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,你可能想知道什么時候/為什么要用Apache Camel來構(gòu)建REST服務(wù)。簡單的答案是,如果你已經(jīng)在使用Apache Camel來整合不同協(xié)議和應(yīng)用程序之間的數(shù)據(jù),那么REST是你需要支持的另一個數(shù)據(jù)源,而不是用Spring Boot或任何其他框架來構(gòu)建REST服務(wù)。你可以利用Camel REST組件來暴露REST API,并使用已知的Camel DSL來消費/生產(chǎn)消息,這有助于你規(guī)范技術(shù)樁。你還可以擴(kuò)展Camel REST,使其包括Swagger,以便使用camel-swagger 組件提供API規(guī)范。

到此這篇關(guān)于使用ApacheCamel表達(dá)REST服務(wù)的方法的文章就介紹到這了,更多相關(guān)ApacheCamel的REST服務(wù)內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

香港服務(wù)器租用

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部