Versions Compared

Key

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

...

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. Notice if you use servlet component then the port number configured here does not apply, as the port number in use is the actual port number the servlet component is using. eg if using Apache Tomcat its the tomcat http port, if using Apache Karaf its the HTTP service in Karaf that uses port 8181 by default etc. Though in those situations setting the port number here, allows tooling and JMX to know the port number, so its recommended to set the port number to the number that the servlet engine uses.
contextPath Sets a leading context-path the REST services will be using. This can be used when using components such as SERVLET where the deployed web application is deployed using a context-path.
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.
skipBindingOnCustomErrortrueCamel 2.14.1: Whether to skip binding on output if there is a custom HTTP error code header. This allows to build custom error messages that do not bind to json / xml etc, as success messages otherwise will do. See further below for an example.
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.

...

You can set multiple options of the same level, so you can can for example configure 2 component options, and 3 endpoint options etc.

Defining a custom error message as-is

If you want to define custom error messages to be sent back to the client with a HTTP error code (eg such as 400, 404 etc.) then from Camel 2.14.1 onwards you just set a header with the key Exchange.HTTP_RESPONSE_CODE to the error code (must be 300+) such as 404. And then the message body with any reply message, and optionally set the content-type header as well. There is a little example shown below:

Code Block
                restConfiguration().component("restlet").host("localhost").port(portNum).bindingMode(RestBindingMode.json);
                // use the rest DSL to define the rest services
                rest("/users/")
                    .post("lives").type(UserPojo.class).outType(CountryPojo.class)
                        .route()
                            .choice()
                                .when().simple("${body.id} < 100")
                                    .bean(new UserErrorService(), "idToLowError")
                                .otherwise()
                                    .bean(new UserService(), "livesWhere");

In this example if the input id is a number that is below 100, we want to send back a custom error message, using the UserErrorService bean, which is implemented as shown:

Code Block
public class UserErrorService {
    public void idToLowError(Exchange exchange) {
        exchange.getIn().setBody("id value is too low");
        exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "text/plain");
        exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 400);
    }
}

In the UserErrorService bean we build our custom error message, and set the HTTP error code to 400. This is important, as that tells rest-dsl that this is a custom error message, and the message should not use the output pojo binding (eg would otherwise bind to CountryPojo).

Integration a Camel component with Rest DSL

...