Raml Java Client Generator
Clone or download
ullgren and machaval Improved readme.md to include information on requirment to add maven …
…repository and pluginRepository. Thanks to Voicu Hodrea for wording. Closes: #36 (#37)
Latest commit 2f69d56 Dec 6, 2018

readme.md

RAML Client Generator

alt text

This tool generates a java rest client for a raml based api using a resource api approach. Supports both 0.8 and 1.0 versions of Raml

Example

For this api

#%RAML 0.8
title: Client API
version: 0.1
baseUri: http://mycompany.com/clientservice/api
documentation:
  - title : Users Platform
    content : This api describes how to access to the users platform
mediaType: application/json
/users:
  description: "Users in the platform"
  get:
    description: "Returns the list of all users"
    responses:
      200:
        body:
          application/json:
            example: |
             [{"user" : "Mariano"}]

Using the generated api

final ClientAPIResponse<List<UsersGETResponseBody>> result = ClientAPIClient.create().users.get();

Customizing the client

final ClientAPIClient client = new ClientAPIClient() {
    @Override
    protected Client getClient() {
        final Client client = ClientBuilder.newClient();
        client.property(ClientProperties.CONNECT_TIMEOUT, 1000);
        client.property(ClientProperties.READ_TIMEOUT, 1000);
        return client;
    }
};
client.users.userId("luis").get();

Calling the code generator from Java

It can easily be embedded the code generation into your code just. Add the dependency to pom.xml

  <dependency>
        <groupId>org.mule.raml.codegen</groupId>
        <artifactId>raml-client-generator-core</artifactId>
        <version>0.2</version>
  </dependency>

Note: Since the RAML Java Client Generator artifacts are not published to Maven Central you will also have to add the following repository either to your pom.xml or an active profile in your maven settings.

  <repositories>
    <repository>
      <id>mulesoft-releases</id>
      <name>Mule Release Repository</name>
      <url>https://repository-master.mulesoft.org/nexus/content/repositories/releases</url>
    </repository>
  </repositories>

And then call the RamlJavaClientGenerator

 new RamlJavaClientGenerator(
                "com.acme",
                targetFolder).generate(this.getClass().getClassLoader().getResource("simple/basic.raml"));

Generate client code from RAML using the maven plugin

There is also a maven plugin that allows you to generate the client code during your build process.

    <build>
        <plugins>
            <plugin>
                <groupId>org.mule.raml.codegen</groupId>
                <artifactId>raml-client-generator-maven-plugin</artifactId>
                <version>0.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate-client</goal>
                        </goals>
                        <configuration>
                            <basePackage>org.mule.example</basePackage>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Note: Since the RAML Java Client Generator artifacts are not published to Maven Central you will also have to add the following plugin repository either to your pom.xml or an active profile in your maven settings.

  <pluginRepositories>
    <pluginRepository>
      <id>mulesoft-releases</id>
      <name>Mule Release Repository</name>
      <url>https://repository-master.mulesoft.org/nexus/content/repositories/releases</url>
    </pluginRepository>
  </pluginRepositories>