Generating JAX-RS from RAML

In the previous section, we have seen how to generate the RAML documentation for the REST resource classes present in a JAX-RS application. In this section, you will learn how to generate the JAX-RS resources classes from an RAML documentation file. This feature is enabled via the raml-to-jaxrs-maven-plugin Maven plugin.

Perform the following steps for using RAML to the JAX-RS Maven plugin in a Maven project:

  1. The raml-to-jaxrs-maven-plugin Maven plugin is available in the Maven central repository. If you do not see the latest release here, clone the Git repository available at https://github.com/mulesoft/raml-for-jax-rs and build the raml-for-jax-rs/raml-to-jaxrs project by executing mvn install.
  2. Go to the maven project where you want to generate the JAX-RS resource classes from the RAML file. Include raml-to-jaxrs-maven-plugin in the project's pom.xml file. The <plugin> entry in the pom.xml file will look like the following:
<plugins> 
         <plugin>
<groupId>org.raml.jaxrs</groupId>
<artifactId>raml-to-jaxrs-maven-plugin</artifactId>
<version>2.1.0</version>
<configuration>
<ramlFile>${basedir}/src/main/resources/raml/department-resource.raml</ramlFile>
<resourcePackage>com.packtpub.rest.ch7.service</resourcePackage>
<modelPackage>com.packtpub.rest.ch7.model</modelPackage>
<jsonMapper>jackson2</jsonMapper>
<jsonMapperConfiguration>
<includeHashcodeAndEquals>true</includeHashcodeAndEquals>
</jsonMapperConfiguration>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
</plugin>
<!-- other plugins go here --> </plugins>
  1. Keep the RAML file that you want to convert into JAX-RS in the resources folder of the maven project. The plugin configuration lets you specify the RAML file via the <ramlFile> element.
  1. Run mvn compile or mvn package, which will result in the generation of the JAX-RS resource classes for the RAML file present in the source folder. You can find the newly generated source files in the generated-sources folder under Maven's target folder.
Note that all the generated JAX-RS resource classes that you see here are just template classes derived from the RAML file. It is your responsibility to add the appropriate business logic to each resource method and make the necessary changes to make it production-ready.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.182.73