Introduction

Introduction to using javalin-generator to generate web routes for Javalin.

Getting started

Maven

Add dinject and dinject-controller dependencies.

<dependency>
  <groupId>io.dinject</groupId>
  <artifactId>dinject</artifactId>
  <version>1.1</version>
</dependency>

<dependency>
  <groupId>io.dinject</groupId>
  <artifactId>dinject-controller</artifactId>
  <version>1.1</version>
</dependency>

Add the APT Annotation processors dinject-generator and javalin-generator typically as scope provided.

<dependency>
  <groupId>io.dinject</groupId>
  <artifactId>dinject-generator</artifactId>
  <version>1.1</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>io.dinject</groupId>
  <artifactId>javalin-generator</artifactId>
  <version>1.1</version>
  <scope>provided</scope>
</dependency>

@Controller

Create controllers with @Path and @Controller. The javalin-generator will generate source code for mapping web routes to the controller method.

@Controller
@Path("/hello")
class HelloController {

  private final MyService myService;

  @Inject
  HelloController(MyService myService) {
    this.myService = myService;
  }

  @Get("/:id")
  HelloDto getById(int id, String otherParam) {
    ...
    return new HelloDto(...);
  }


  @Post
  void save(HelloDto dto, Context context) {
    // save hello data ...
  }

  @Get
  List<HelloDto> getAll() {
    return myService.findAll();
  }

}

The controllers can have dependencies as long as they are @Singleton (and DInject will then add them to the context used for dependency injection).

@Get

Annotate methods on the controller with @Get, @Put, @Post, @Delete or @Patch. Methods with these annotations will be mapped to web routes.

Context

Methods can use io.javalin.Context as a method argument. The web route will pass the context on to the controller method. We want to do this when we want full access including things like headers, cookies etc.

@Post
void save(HelloDto dto, Context context) {
  // use javalin context, get cookies, headers ...
}

Path type conversions

There are built in type conversions for path and query parameters for the following types:

  • int, long, short, float, double, boolean, Integer, Long, Short, Float, Double, Boolean
  • BigDecimal, UUID, LocalDate, LocalTime, LocalDateTime

If an argument fails type conversion then InvalidPathArgumentException is thrown. This exception could be mapped to a 404 response in the exception handler.

Note that path conversions imply the value can NOT be null. Query parameters on the other hand effectively allow null (not specified) values - query parameters are deemed optional.

@Roles

An application can define a @Roles annotation. If the annotation matches the name "Roles" then it is deemed to define application security roles and generates appropriate code in the web route.

@Roles({ANYONE})
@Get
List<HelloDto> getAll() {
  ...
}
ApiBuilder.get("/hello", ctx -> {
  ctx.json(controller.getAll());
  ctx.status(200);
}, roles(ANYONE));