JVM - api.route()

Creates a new route (path) within an API.

import io.nitric.Nitric;

public class Application {
  public static void main(String[] args) {
    var api = Nitric.INSTANCE.api("public");

    var customersRoute = api.route("/customers");

    Nitric.INSTANCE.run();
  }
}

Parameters

  • Name
    match
    Required
    Required
    Type
    String
    Description

    The path matcher to use for this route. Calling route on the same API more than once with the same matcher will return the same route object. Matchers accept path parameters in the form of a colon prefixed string. The string provided will be used as that path parameter's name when calling middleware and handlers. See create a route with path params

  • Name
    options
    Optional
    Optional
    Type
    RouteOptions
    Description

    Additional options when creating method.

    • Name
      middleware
      Optional
      Optional
      Type
      HttpMiddleware or List<HttpMiddleware>
      Description

      Middleware to apply to all methods on this route.

    • Name
      security
      Optional
      Optional
      Type
      Map<String, List<String>>
      Description

      Specify the security rules that apply to this route. Overrides API level security rules.

Notes

The middleware property on the options param is useful for applying universal middleware such as CORS headers or Auth, across an entire route. However, if methods aren't registered, the route won't be deployed. If you need to run the same handler for all methods on a route, you should use route.all()

Examples

Create a route

import io.nitric.Nitric;

public class Application {
  public static void main(String[] args) {
    var api = Nitric.INSTANCE.api("public");

    var customersRoute = api.route("/customers");

    Nitric.INSTANCE.run();
  }
}

Create a route with path params

Route paths can include dynamic parameters. These values will automatically be parsed and provided in the context object for your middleware and handlers as a string.

For example, if you have a customers path and you want to include a customerId param you would define the route like this.

import io.nitric.Nitric;

public class Application {
  public static void main(String[] args) {
    var api = Nitric.INSTANCE.api("public");

    var customersRoute = api.route("/customer/:customerId");

    Nitric.INSTANCE.run();
  }
}

Create a route with middleware

import io.nitric.Nitric;
import io.nitric.resources.RouteOptions;

public class Application {
  public static void main(String[] args) {
    var api = Nitric.INSTANCE.api("public");

    var customersRoute = api.route("/customers", new RouteOptions.Builder()
      .middleware(authMiddleware)
      .build()
    );

    Nitric.INSTANCE.run();
  }
}