欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2022年06月10日 14:29:10   作者:方石劍  
Apache Camel可以作為一個獨(dú)立的或嵌入的庫在任何地方運(yùn)行,它可以幫助整合,這篇文章主要介紹了如何使用Apache?Camel表達(dá)REST服務(wù),需要的朋友可以參考下

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

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

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

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

大家都知道,Camel的主要功能是路由引擎。路由可以使用基于Java的DSL或基于XML來開發(fā)。在這篇文章中,我將按照J(rèn)avaDSL來開發(fā)一個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路由類似,但使用rest() 。我們需要提到用于暴露端點(diǎn)的組件服務(wù)。Camel支持以下組件來實(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");

如何覆蓋端口

你可以用你選擇的任何其他端口號來覆蓋默認(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。為了實(shí)現(xiàn)這一點(diǎn),你需要將綁定模式設(shè)置為restConfiguration() ,因?yàn)?code>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驗(yàn)證器組件驗(yàn)證傳入的請求,這需要在您的Maven POM中添加camel-bean-validator 依賴關(guān)系。

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-bean-validator</artifactId>
</dependency>

在請求對象中定義驗(yàn)證規(guī)則

為了實(shí)現(xiàn)輸入請求驗(yàn)證,你需要為POJO/請求類中的字段添加驗(yàn)證注解。這些注釋可在包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來消費(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關(guān)文章

  • centos6.5下svn的使用說明

    centos6.5下svn的使用說明

    這篇文章主要介紹了centos6.5下svn的使用說明的相關(guān)資料,方法非常的實(shí)用,有需要的小伙伴可以參考下
    2016-10-10
  • Linux中的awk命令使用詳解

    Linux中的awk命令使用詳解

    這篇文章主要介紹了Linux中的awk命令使用詳解的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • linux寫shell需要注意的問題(必看)

    linux寫shell需要注意的問題(必看)

    下面小編就為大家?guī)硪黄猯inux寫shell需要注意的問題(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • PHP程序員玩轉(zhuǎn)Linux系列 使用supervisor實(shí)現(xiàn)守護(hù)進(jìn)程

    PHP程序員玩轉(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-04
  • 修改linux文件權(quán)限命令:chmod命令詳解

    修改linux文件權(quán)限命令:chmod命令詳解

    本篇文章主要介紹了修改linux文件權(quán)限命令:chmod,Linux系統(tǒng)中的每個文件和目錄都有訪問許可權(quán)限,用它來確定誰可以通過何種方式對文件和目錄進(jìn)行訪問和操作。
    2016-12-12
  • Linux系統(tǒng)網(wǎng)卡設(shè)置教程

    Linux系統(tǒng)網(wǎng)卡設(shè)置教程

    這篇文章主要介紹了Linux系統(tǒng)網(wǎng)卡的設(shè)置教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • linux的基本命令mkdir使用詳解

    linux的基本命令mkdir使用詳解

    這篇文章主要介紹了linux的基本命令mkdir使用詳解的相關(guān)資料,需要的朋友可以參考下
    2023-03-03
  • 詳解linux中的strings命令簡介

    詳解linux中的strings命令簡介

    本篇文章主要介紹了linux中的strings命令簡介,在linux下搞軟件開發(fā)的朋友, 幾乎沒有不知道strings命令的。非常具有實(shí)用價(jià)值,需要的朋友可以參考下。
    2016-12-12
  • Linux中g(shù)it用https連接時不用每次輸入密碼的方法

    Linux中g(shù)it用https連接時不用每次輸入密碼的方法

    這篇文章主要給大家介紹了關(guān)于Linux中g(shù)it使用https連接時不用每次輸入密碼的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • Apache?Pulsar集群搭建部署詳細(xì)過程

    Apache?Pulsar集群搭建部署詳細(xì)過程

    這篇文章主要介紹了Apache?Pulsar集群搭建過程,搭建Pulsar集群至少需要3個組件:ZooKeeper集群、BookKeeper集群和Broker集群,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02

最新評論