Versions Compared

Key

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

...

Code Block
languagejava
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                rest("/say/hello")
                    .get("/hello").to("direct:hello");
                rest("/say/bye")
                    .get("/bye").consumes("application/json").to("direct:bye")
                    .post("/bye").to("mock:update");

                from("direct:hello")
                    .transform().constant("Hello World");
                from("direct:bye")
                    .transform().constant("Bye World");
            }
        };
    }

...

This defines a REST service with the following url mappings:

Base PathUri templateVerbConsumes
/say/hellogetall
/say/byegetapplication/json
/say/byepostall

Notice that in the REST service we route directly to a Camel endpoint using the to(). This is because the Rest DSL has a short-hand for routing directly to an endpoint using to(). An alternative is to embed a Camel route directly using route() - there is such an example further below.

...

Code Block
  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <rest uripath="/say/hello">
       <get><get uri="/hello">
        <to uri="direct:hello"/>
      </get>
    </rest>
  <get  <rest uri="/say/bye">
      <get consumes="application/json">
        <to uri="direct:bye"/>
      </get>
       <post><post uri="/bye">
        <to uri="mock:update"/>
      </post>
    </rest>
    <route>
      <from uri="direct:hello"/>
      <transform>
        <constant>Hello World</constant>
      </transform>
    </route>
    <route>
      <from uri="direct:bye"/>
      <transform>
        <constant>Bye World</constant>
      </transform>
    </route>
  </camelContext>

 

Using base path

...

The REST DSL allows to define base path prefixes to make the DSL a bit more DRY. For example to define a customer path, we can set the prefix base path in rest("/customer") and then provide the past postfix uri templates in the verbs, as shown below:

...

And using XML DSL it becomes:

Code Block
    <rest uripath="/customers/">
      <get uri="/{id}">
        <to uri="direct:customerDetail"/>
      </get>
      <get uri="/{id}/orders">
        <to uri="direct:customerOrders"/>
      </get>
      <post uri="/neworder">
        <to uri="direct:customerNewOrder"/>
      </post>
    </rest>
Tip

The REST DSL will take care of duplicate path separators when using base path prefixes. In the example above the rest path prefix ends with a slash ( / ) and the verb starts with a slash ( / ). But Apache Camel will take care of this and remove the duplicated slash.and uri templates. In the example above the rest base path ends with a slash ( / ) and the verb starts with a slash ( / ). But Apache Camel will take care of this and remove the duplicated slash.

It is not required to use both base path and uri templates. You can omit the bast path and define the base path and uri template in the verbs only. The example above can be defined as:

Code Block
    <rest>
      <get uri="/customers/{id}">
        <to uri="direct:customerDetail"/>
      </get>
      <get uri="/customers/{id}/orders">
        <to uri="direct:customerOrders"/>
      </get>
      <post uri="/customers/neworder">
        <to uri="direct:customerNewOrder"/>
      </post>
    </rest>

Embedding Camel routes

Each of the rest service becomes a Camel route, so in the first example we have 2 x get and 1 x post REST service, which each become a Camel route. And we have 2 regular Camel routes, meaning we have 3 + 2 = 5 routes in total. 

...

Code Block
// configure to use restlet on localhost with the given port
// and enable auto binding mode
restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.auto);

// use the rest DSL to define the rest services
rest("/users/")
    .post("new").type(UserPojo.class)
        .to("direct:newUser");

...

Code Block
// configure to use restlet on localhost with the given port
// and enable auto binding mode
restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.auto);

// use the rest DSL to define the rest services
rest("/users/")
    .post("new").type(UserPojo.class).outType(CountryPojo.class)
        .to("direct:newUser");

...

By having the JAXB annotations the POJO supports both json and xml bindings. 

Configuring Rest DSL

The Rest DSL allows to configure the following options using a builder style 

OptionDefaultDescription
component The Camel Rest component to use for the REST transport, such as restlet, spark-rest. If no component has been explicit configured, then Camel will lookup if there is a Camel component that integrates with the Rest DSL, or if a org.apache.camel.spi.RestConsumerFactory is registered in the registry. If either one is found, then that is being used.
schemehttpThe scheme to use for exposing the REST service. Usually http or https is supported
hostname The hostname to use for exposing the REST service.
port The port number to use for exposing the REST service.
restHostNameResolverlocalHostNameIf no hostname has been explicit configured, then this resolver is used to compute the hostname the REST service will be using. The resolver supports localHostName or localIp.
bindingModeoffWhether binding is in use. See further above for more details.
jsonDataFormat Name of specific json data format to use. By default json-jackson will be used. Notice: Currently Jackson is what we recommend and are using for testing.
xmlDataFormat Name of specific XML data format to use. By default jaxb will be used. Notice: Currently only jaxb is supported.
componentProperty Allows to configure as many additional properties. This is used to configure component specific options such as for Restlet / Spark-Rest etc.
endpointProperty Allows to configure as many additional properties. This is used to configure endpoint specific options for  Restlet / Spark-Rest etc.
consumerProperty Allows to configure as many additional properties. This is used to configure consumer specific options for  Restlet / Spark-Rest etc.
dataFormatProperty Allows to configure as many additional properties. This is used to configure the data format specific options. For example set property prettyPrint to true to have json outputted in pretty mode.

...