The Recipe for Angular in a Java EE Environment: Aot Build

DZone 's Guide to

The Recipe for Angular in a Java EE Environment: Aot Build

In the final part of this series, we go over the XML you need to implement URL rewriting, and the Java you need to incorporate Swagger into your REST API.

· Web Dev Zone ·
Free Resource

In this additional part of the series "The Recipe for Angular 2 in a Java EE Environment" I want to show how to implement an Aot Build for the UI. The Angular2AndJavaEE project uses the Ahead of Time compilation feature of Angular to minimize size and maximize speed. Aot needs URL rewriting and to access the right application for the selected language. For that, the tuckey urlrewriter is used. Then I show how to add Swagger to the REST API for easy testing of the REST Interface. 

Web Module Build

The Web Module Build has been explained in the article about the Maven build. Here I will explain steps 4 and 6 that have been enhanced to support the Aot Build.

Step 4

The Aot build compiles in the translations and templates into one optimized package. That means you get one package per supported language. These packages need to be put in different directories. In this case, the directories en/ and de/. To build the two packages, Angular-CLI needs to be executed twice.

         <id>angular-cli build de</id>
         <id>angular-cli build en</id>

In line 30, we have the unique id for the execution.

In line 36, the Angular-CLI executable is provided.

In line 38, Angular-CLI gets the build parameter.

In lines 39 and 40,  the --prod --aot parameters are set to switch on the ahead of time compilation.

In lines 41 and 42, the base-href is set to make the routing work. It needs to be set to the right package directory.

In lines 43 and 44, the output-path is set to build the packages into different directories.

In lines 45 and 46, the locale of the package is set for translation.

In line 47, the i18nFile is provided to enable the translations to be compiled.

Step 6

In Step 6, the war is built and the directories en/ and de/ with the packages need to be included.

<?xml version="1.0" encoding="UTF-8"?>

In line 10, the working directory is set where Angular-CLI has built the packages.

In lines 12 and 13, the English and German packages are included in the war.

The Swagger directory is included because it is inside the web app directory.

URL Rewriting

To make the reload functionality and links within the application work, URL rewriting is required. With URL rewriting, the requests are sent to the right Angular application for the language. To include the URL rewrite filter, this web.xml is needed:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

In lines 3 and 4, the filter is included.

In lines 10 and 11, the path to the config file of the urlrewriter is set.

In lines 14 and 15, the access to the status page is set. The status page is found at http://localhost:8080/carrental-web/rewrite-status  

In linse 18-22, the filter mapping that processes all requests is set.

To configure the URL filter, this urlrewrite.xml is needed:


In line 3, the regular expression matches requests with /de/ in the URL that does not include a ".". This forwards Paths but not Files.

In line 4, the request is forwarded to the German Angular application.

In lines 7 and 8, the same is done for English.

The regular expressions work for the application but they deserve some work to make them more robust and reusable.


Swagger is a UI interface to test REST services. Swagger for Jax-Rs uses additional annotations to generate a JSON file that specifies the Swagger UI. The JSON file can be found at http://localhost:8080/carrental-web/rest/swagger.json. To add Swagger, the Swagger directory is included in the in the war and Swagger is added to the application class:

public class CrApplication extends Application {

public CrApplication() {
   BeanConfig beanConfig = new BeanConfig();
   beanConfig.setSchemes(new String[]{"http"});

public Set<Class<?>> getClasses() {
   Set<Class<?>> classes = new HashSet<Class<?>>();
   return classes;


In line 5, a new beanConfig is made.

In line 6, the swagger jax-rs version is set.

In line 7, the scheme is set to HTTP.

In line 8, the host is set to the port. Currently, Swagger only works on app servers with the HTTP port 8080. If you need a different port, it needs to be changed here.

In line 9, the base path for the REST calls is set. 

In line 10, the package with the resource classes is set. This Swagger package searches for the annotations in the classes.

Inl ine 11, the parameter scan is set to 'true' to make Swagger scan its annotations.

In lines 24-25, the Swagger classes for Jax-Rs are added.

To add Swagger support to a resource class the annotations @Api and @ApiOperation have to be added:

@Produces({ "application/json" })
public class CrTableResource {
   private CrTableService service;

   @ApiOperation(value="gets the rows for the table", response=CrTableRow.class, responseContainer="List")
   public Response getAll(@PathParam("mietNr") final String mietNr, @HeaderParam("Origin") final String origin,
      @HeaderParam("Accept-Language") final String acceptLang) {
      String[] langs = acceptLang.split(",");
      Locale locale = Locale.forLanguageTag(langs[0]);
      if (origin != null && origin.contains("http://localhost")) {
         return Response.ok(service.readCrRowsByMiete(mietNr, locale)).header("Access-Control-Allow-Origin", "*")
            "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept")
            .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS").allow("OPTIONS").build();
      } else {
      return Response.ok(service.readCrRowsByMiete(mietNr, locale)).build();

In line 4, the annotation @Api is added with the title of the resource.

In line 12, the annotation @ApiOperation is added with title and response metadata.


The GitHub project Angular2AndJavaEE now has all the features that were planned. The Aot build is included and Swagger is available. The project shows some important features of Angular and Java EE and demonstrates that Angular can fit in a corporate environment. Angular provides an easy to use and powerful UI framework that can be integrated with Java EE. That makes it a good fit for future UI development in the Java EE world. The project will continue to be maintained. The news of the project can be found in the Project Diary.

java ee ,angular 4 ,angular cli ,swagger ,web dev

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}