Lab 6 - REST Web Services

RH_Icon_Compass_Shiny.png In this lab you will create a route that exposes a REST Web Service using the REST DSL.
general_info_polished.png Apache Camel supports multiple approaches to defining REST services. In particular, Apache Camel provides the REST DSL (Domain Specific Language), which is a simple but powerful fluent API that can be layered over any REST component and provides integration with Swagger.
  1. Select File -> Import.. and select General -> Existint Project into Workspace
  2. Click on Select archive file and browse to ~/FuseWorkshop/support/Lab6, and select Lab6.zip
  3. Click Finish.
  4. Double-click on the blueprint.xml file under Camel Contexts.
  5. Click on the Source tab.
  6. Copy and paste the following xml lines before <camelContext>
<service interface="javax.servlet.http.HttpServlet"><service-properties> <entry key="alias" value="/api-docs/*"/> <entry key="init-prefix" value="init."/> <entry key="init.base.path" value="http://localhost:8182"/&gt; <entry key="init.api.path" value="http://localhost:8181/api-docs"/&gt; <entry key="init.cors" value="true"/> <entry key="init.api.title" value="Lab6 Camel REST Example"/> <entry key="init.api.version" value="1.2"/> <entry key="init.api.description" value="Camel Rest Example with Swagger"/></service-properties><bean class="org.apache.camel.component.swagger.DefaultCamelSwaggerServlet" /></service>
general_info_polished.png These XML lines are used to configure the Swagger service.
  1. Copy and paste the following XML lines after <camelContext>
<restConfiguration component="jetty" port="8182" host=”0.0.0.0”/>
general_info_polished.png Apache Camel offers a REST styled DSL which can be used with Java or XML. The intention is to allow end users to define REST services using a REST style with verbs such as get, post, delete etc.
  1. Save the file (Ctrl+S).

  1. Click on the Design tab.
  2. Add a Direct component from the Components palette.

  3. Edit its properties

  4. Advanced -> Path -> Name: weather

general_info_polished.png The direct component provides direct, synchronous invocation of any consumers when a producer sends a message exchange. This endpoint can be used to connect existing routes in the same camel context.
  1. Add a setHeader component from the Transformation palette.

  2. Edit its properties

  3. Expression: q=${header.city}&appid=cb2136e261373990f015acae72f6178b

  4. Header Name: CamelHttpQuery

  5. Add another setHeader component from the Transformation palette.

  6. Edit its properties

  7. Expression: data/2.5/weather/

  8. Header Name: CamelHttpPath

  9. Link Direct to the first setHeader.

  10. Link the first setHeader to the second setHeader.
  11. Add a Generic component from the Components palette.

  12. Edit its properties

  13. Uri: http4://api.openweathermap.org?bridgeEndpoint=true

  14. Link the second setHeader to the Http4 component.

  15. Save the file (Ctrl+S).

general_info_polished.png The http4 component provides HTTP-based endpoints for calling external HTTP resources (as a client to call external servers using HTTP).
  1. Select the Routes menu, and click on Add Route.
  2. Add a Direct component from the Components palette.

  3. Edit its properties

  4. URI: direct:append

  5. Add a File component from the Components palette.

  6. Edit its properties

  7. Advanced -> Path -> Directory Name: appendDir

  8. Advanced -> General -> File Name: append.txt
  9. Advanced -> Producer -> File Exist: Append

  10. Add a setBody component from the Transformation palette.

  11. Edit its properties

  12. Expression: {"result": "OK"}

  13. Link Direct to File.

  14. Link File to setBody.
  15. Save the file (Ctrl+S).

general_info_polished.png As with the CXF web service, the last step of the route returns a message to the caller. So in the first route we return the response from the http call, and in the second route we set the body to a Json response.
  1. Right-click on the JBoss Fuse server, and select Add and Remove
  2. Select Lab6 project, click Add and Finish.
  3. Open a web browser, and go to http://localhost:8181/hawtio-swagger/index.html
  4. In the text box, enter the following API URL: http://localhost:8181/api-docs
  5. Click on the Explore button.

  1. Click on the List Operations link to show the available REST operations.
  2. Click on the /lab6/weather/{city} link to expand it.
  3. Enter Buenos Aires,AR as the city parameter and click Try it out!

  1. You have successfully tested the first of the two routes.
  2. Click on the /lab6/append link to expand it.
  3. Enter any text in the body parameter.
  4. Click on the Try it out! Button.

  1. Close the web browser.
  2. Open a terminal or a file explorer and go to ~/FuseWorkshop/Fuse/appendDir.
  3. Check out the contents of the append.txt file.
  4. Back in JBDS go to the Servers tab, click on the Lab6 project under JBoss Fuse Server, and click Remove.

results matching ""

    No results matching ""