이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 157. Spark REST


Spark REST Component

Available as of Camel 2.14
The Spark-rest component allows to define REST endpoints using the Spark REST Java library using the Rest DSL.
Important
Spark Java requires Java 8 runtime.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-spark-rest</artifactId>
    <version>${camel-version}</version>
</dependency>
Copy to Clipboard Toggle word wrap

URI format

spark-rest://verb:path?[options]
Copy to Clipboard Toggle word wrap

URI Options

Expand
Name
Default Value
Description
verb
get, post, put, patch, delete, head, trace, connect, or options.
path
the content path which support Spark syntax. See further below for examples.
accept
accept type such as: 'text/xml', or 'application/json'. By default we accept all kinds of types.

Path using Spark syntax

The path option is defined using a Spark REST syntax where you define the REST context path using support for parameters and splat. See more details at the Spark Java Route documentation.
The following is a Camel route using a fixed path
  from("spark-rest:get:hello")
    .transform().constant("Bye World");
Copy to Clipboard Toggle word wrap
And the following route uses a parameter which is mapped to a Camel header with the key "me".
  from("spark-rest:get:hello/:me")
    .transform().simple("Bye ${header.me}");
Copy to Clipboard Toggle word wrap

Mapping to Camel Message

The Spark Request object is mapped to a Camel Message as a org.apache.camel.component.sparkrest.SparkMessage which has access to the raw Spark request using the getRequest method. By default the Spark body is mapped to Camel message body, and any HTTP headers / Spark parameters is mapped to Camel Message headers. There is special support for the Spark splat syntax, which is mapped to the Camel message header with key splat.
For example the given route below uses Spark splat (the asterisk sign) in the context path which we can access as a header form the Simple language to construct a response message.
  from("spark-rest:get:/hello/*/to/*")
    .transform().simple("Bye big ${header.splat[1]} from ${header.splat[0]}");
Copy to Clipboard Toggle word wrap

Rest DSL

Apache Camel provides a new Rest DSL that allow to define the REST services in a nice REST style. For example we can define a REST hello service as shown below:
  return new RouteBuilder() {
      @Override
      public void configure() throws Exception {
            rest("/hello/{me}").get()
                .route().transform().simple("Bye ${header.me}");
        }
    };
Copy to Clipboard Toggle word wrap
  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <rest uri="/hello/{me}">
      <get>
        <route>
          <transform>
            <simple>Bye ${header.me}</simple>
          </transform>
        </route>
      </get>
    </rest>
  </camelContext>
Copy to Clipboard Toggle word wrap
See more details at the Rest DSL.

More examples

There is a camel-example-spark-rest-tomcat example in the Apache Camel distribution, that demonstrates how to use camel-spark-rest in a web application that can be deployed on Apache Tomcat, or similar web containers.
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat