Skip to content

Generating Java Code From RADL

Rémon (Ray) Sinnema edited this page Jun 17, 2015 · 2 revisions

Understanding The Code Generation Process

The code generation process consists of the following steps:

  1. Inspect the RADL file
  2. Determine what code should be there to implement the RADL file
  3. Compare that with what code currently is there
  4. Make the necessary changes to arrive at the desired state

Currently, only Spring code can be generated.

The intent is to allow two-way changes: both the RADL file and the code can be changed at any time. To support that, the code is divided into two parts:

  • Generated code is under control of the code generation process
    This code will be created if it doesn't exist, or completely replaced if it does. Any manual changes will be lost.
    Generated code consists of controllers and constants for URIs, media types, etc.
  • Skeleton code is under control of the developer
    This code will be created if it doesn't exists, but left alone when it does. The developer is free to make changes.

Generated code consists of services. The controllers are @Autowired to the services. Where the controllers define the REST "contract" using Spring's @RequestMapping annotation, the services actually implement the functionality.

Running The Code Generation Process

The code generation process is implemented in the radl.java.generation.spring.RadlToSpringServer class. This class supports the following arguments:

  • The RADL file to process
  • The base directory for the project. This defaults to the current directory
  • The package prefix for the generated code. This defaults to radl.sample
  • The directory for the generated code, relative to the base directory. This defaults to build/src/java
  • The directory for the skeleton code, relative to the base directory. This defaults to src/main/java
  • The source code management (SCM) system to use. This defaults to default. The SCM is used to prepare files for update and/or delete, which is necessary when the SCM makes files read-only, like Perforce does. To use the Perforce SCM, supply p4. The default SCM does nothing
  • The header to use for generated files. This defaults to Generated by RADL.