Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The Spark-rest component allows to define REST endpoints using the Spark REST Java library which has a nice DSL (not to be mistaken with Apache Spark) using the Rest DSL.

Info

This component integrates with Spark REST Java library which is a REST library. This is not related to Apache Spark which is a project about big data.

Apache Camel also provides a Camel component (camel-spark) for integrating Camel with Apache Spark.

Info
Spark REST Java Library requires Java 8 runtime.

Maven users will need to add the following dependency to their pom.xml for this component:

...

Code Block
  spark-rest://verb:path?[options]

URI Options

...

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.

...

Code Block
  from("spark-rest:get:/hello/*/to/*")
    .transform().simple("Bye big ${header.splat[1]} from ${header.splat[0]}");

SparkRouteBuilder

If you use Java code, then you can use the class org.apache.camel.component.sparkrest.SparkRouteBuilder to define routes using a Spark DSL, as shown below

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:

Tabs Container
idRestDslTabs
titleRest DSL
directionhorizontal
Tabs Page
idRestDslTabsJava
titleJava

...

Code Block
  return new 
SparkRouteBuilder
RouteBuilder() {
      @Override
      public void configure() throws Exception {
            
get
rest("/hello/
:
{me}").get()
                .route().transform().simple("Bye ${header.me}");
        }
    };
Tabs Page
idRestDslTabsXml
titleXML
Code Block
xml
xml
  <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>

See more details at the Rest DSLWhen using the SparkRouteBuilder, then you can define Camel routes using the REST verbs, such as get, post, put, delete etc.

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.

Include Page
Endpoint See Also
Endpoint See Also