Contract first Rest API

As an integration consultant I have an extensive background in web services. To be specific SOAP enabled web services. The way I used to create a service was by first defining the contract, the WSDL and the XSD files. Based on those files I could generate a service without the need for me to manually type any code.

However lately I started working with Rest services, both JSON and XML. I have written a post (Rest just got easy) on how to create a service using the rest DSL from camel. However in some situations you might want to use a JAX-RS implementation, for example using CXFRS. This means you have to first create the interface definition using POJO’s. You could do this the manual way, like me in my first couple of implementations. Or you could generate the code based on an interface design (Hooray for the lazy!). In this blog post I will show you how to generate your rest interface implementation using swagger documentation (Swagger in a nutshell).

Using a maven plugin you can easily generate the required code based on a yaml styled swagger specification file. The plugin can be found through this dependency:

<dependency>
   <groupId>io.swagger</groupId>
   <artifactId>swagger-codegen-maven-plugin</artifactId>
   <version>2.1.6</version>
</dependency>

Because I am using CXFRS I added the dependency for that as well to my pom file:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-cxf</artifactId>
</dependency>

After you have added the dependencies to the pom file you can configure the plugin.

<plugin>
   <groupId>io.swagger</groupId>
   <artifactId>swagger-codegen-maven-plugin</artifactId>
   <version>2.2.0-SNAPSHOT</version>
   <executions>
      <execution>
        <goals>
          <goal>generate</goal>
        </goals>
        <configuration>
		<!-- specify the swagger yaml -->
		<inputSpec>src/main/resources/yaml/swagger.yaml</inputSpec>
		<!-- target to generate -->
		<language>jaxrs-cxf</language>
		<apiPackage>nl.rubix.api</apiPackage>
		<modelPackage>nl.rubix.api.model</modelPackage>
		<configOptions>
		<sourceFolder>src/main/java</sourceFolder>
		</configOptions>
		</configuration>
     </execution>
   </executions>
</plugin>

The plugin has several configuration options.

  • inputSpec – OpenAPI Spec file path
  • language – target generation language
  • output – target output path (default is ${project.build.directory}/generated-sources/swagger)
  • templateDirectory – directory with mustache templates
  • addCompileSourceRoot – add the output directory to the project as a source root (true by default)
  • modelPackage – the package to use for generated model objects/classes
  • apiPackage – the package to use for generated api objects/classes
  • invokerPackage – the package to use for the generated invoker objects
  • configOptions – a map of language-specific parameters (see below)
  • configHelp – dumps the configuration help for the specified library (generates no sources)

As you can see in my example I specified the language to be “jaxrs-cxf”. Meaning that the generated sources will be specific for jaxrs-cxf. The modelPackage will contain the actual objects. If specified the plugin will add the XML declarations to the model POJO’s. The apiPackage will contain the interface specifications.

Create the yaml file on the specified location. My yaml file looks like this:

---
swagger: '2.0'
info:
  version: 0.0.0
  title: Simple API
paths:
  /:
    get:
      responses:
        200:
          description: OK

Meaning, a very simple interface that listens to the root (“/”) of servic epath and will return a http 200 if everything went as planned.

Now that the configuration is done, you can generate the sources with “mvn compile”. You will see the generated sources in the packages you specified.

If everything went correct you can now specify your service. In my example I am using blueprint in combination with the java DSL.

The blueprint definition for the cxfrs component:

<cxf:rsServer id="api" address="http://localhost:9092/test"
               serviceClass="nl.rubix.api.contractfirst.rest.DefaultApi"
               loggingFeatureEnabled="false">
   <cxf:providers>
     <bean class="org.apache.cxf.jaxrs.provider.json.JSONProvider" />
   </cxf:providers>
</cxf:rsServer>

And the route configuration:

from("cxfrs:bean:api?bindingStyle=SimpleConsumer").log("you did it!");

My example project can be found here: Download sources

Advertisements

JBoss Fuse on a raspberry pi 3

I was looking for a small server to run my personal fuse installation on. After a quick search I found the raspberry Pi as a likely candidate. A cheap and energy efficient device. Although it runs on an ARM processor you can run a (ARM) JDK on it. Which allows you to run a Jboss server and thus a Fuse server.

First install an operating system on your Pi. I chose Raspbian (lite), because of its simplicity and the fact that is a Debian distro, which I like. You can get the latest here. Installing it is simple and the site provides a good installation guide.

After the installation you want to add an usb drive or an external HDD. The SD is required for the installation of your OS but it is not wise to install the rest of your software on the SD card. More information about this can be found here. Make sure that your drive is correctly formatted (for example ext4). Check the location of your drive and (auto) mount it via editing fstab.

sudo fdisk –l 
sudo vi /etc/fstab
/dev/sda1		/apps		ext4	user,exec	0	1

After rebooting your pi confirm that the mount has succeeded. When you run “mount -l” you should see the “/dev/sda1/” mounted on the “/apps” location. Change the access rights to the directory with “sudo chown pi:pi /apps”. Note that if you made a mistake you can no longer reach your pi with ssh. It will be started in “safe mode”. You have to access the pi directly and fix the problem.

Now you are all set up to install the JDK and Fuse. Because the raspberry pi has an ARM processer you cannot install the usual JDK. You have to install one for the ARM processor, you can find the installer here.

The installation is quite straightforward, after you have unzipped the JDK you have to set the java version. This can be done in the .profile file.

vi ~/.profile
export JAVA_HOME=&lt;locatie&gt;
export PATH=$PATH:$JAVA_HOME/bin

Conferm that java is working (java –version).

Install the fuse server (installation guide).

The last step is simple, start the fuse server. Mind that it will take a while before it is started, especially the first time.