...
The jetty component provides HTTP-based endpoints for consuming and producing HTTP requests. That is, the Jetty component behaves as a simple Web server.
Jetty can also be used as a http client which mean you can also use it with Camel as a producer.
Info | ||
---|---|---|
| ||
The |
...
In this sample we define a route that exposes a HTTP service at http://localhost:8080/myapp/myservice
:
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jettyjetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteTest.java} |
...
Our business logic is implemented in the MyBookService
class, which accesses the HTTP request contents and then returns a response.
Note: The assert
call appears in this example, because the code is part of an unit test.
Wiki Markup |
---|
{snippet:id=e2|lang=java|url=camel/trunk/components/camel-jettyjetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteTest.java} |
one
, to the endpoint, mock:one
, and all others to mock:other
.Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jettyjetty9/src/test/java/org/apache/camel/component/jetty/JettyContentBasedRouteTest.java} |
http://serverUri?one=hello
, the Jetty component will copy the HTTP request parameter, one
to the exchange's in.header
. We can then use the simple
language to route exchanges that contain this header to a specific endpoint and all others to another. If we used a language more powerful than Simple (such as EL or OGNL) we could also test for the parameter value and do routing based on the header value as well....
The following example shows how to customize the DefaultHttpBinding
in order to change how exceptions are returned:
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jettyjetty9/src/test/java/org/apache/camel/component/jetty/HttpBindingRefTest.java} |
...
You may want to return a custom reply message when something goes wrong, instead of the default reply message Camel Jetty replies with.
You could use a custom HttpBinding
to be in control of the message mapping, but often it may be easier to use Camel's Exception Clause to construct the custom reply message. For example as show here, where we return Dude something went wrong
with HTTP error code 500:
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jettyjetty9/src/test/java/org/apache/camel/component/jetty/JettyOnExceptionHandledTest.java} |
...
From Camel 2.3.0, camel-jetty support to multipart form post out of box. The submitted form-data are mapped into the message header. Camel-jetty creates an attachment for each uploaded file. The file name is mapped to the name of the attachment. The content type is set as the content type of the attachment file name. You can find the example here.
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jettyjetty9/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java|title=Note: getName() functions as shown below in versions 2.5 and higher. In earlier versions you receive the temporary file name for the attachment instead} |
...