Java2Typescript provides a bridge between a Java REST service definition and a Typescript client.
It enables to expose the full DTO model and REST services API as a clean typescript definition file, thus enabling strong type checking on the model of your application.
This project is composed of 3 modules :
- java2typescript-jackson: A Jackson module that generate typescript definition files for Java classes, using a Jackson ObjectMapper.
- java2typescript-jaxrs: An extension to java2typescript-jackson that takes a JAX-RS annotated java class and produces both :
- A Typescript definition file of the service (
.d.ts
), together with description of all needed DTO objects. - An implementation
.js
of the above definition as REST client stub. - java2typescript-maven-plugin: A maven plugin to automate the generation of
.d.ts
and.js
implementation of REST services. - A sample web application that demonstrate the usage of java2typescript
Here is a schema of the workflow for a typical project using j2ts :
There are only two source files here :
- Server side:
AppRest.java
with annotated JAX-RS services - Client side:
App.ts
The detailed workflow is:
AppRest.java
contains the annotated JAX-RS service definition- j2ts compiles the REST service definition into a
.d.ts
description file, and a.js
file (runtime implementation) App.ts
imports and uses the.d.ts
fileApp.ts
is compiled into aApp.js
file (by typescript compiler)
Please refer to the documentation of the maven plugin and the example below
java2typescript handles all the HTTP REST standard itself, and provide REST services as vanilla Typescript methods, regardless of the HTTP method / mime to use.
Consider the following JAX-RS service
@Path( "/people" )
public interface PeopleRestService {
@Produces( { MediaType.APPLICATION_JSON } )
@GET
public Collection< Person > getPeoples( @QueryParam( "page") @DefaultValue( "1" ) final int page ) {
return peopleService.getPeople( page, 5 );
}
@Produces( { MediaType.APPLICATION_JSON } )
@Path( "/{email}" )
@GET
public Person getPeople( @PathParam( "email" ) final String email ) {
return peopleService.getByEmail( email );
}
}
The maven plugin will produce the following typescript definition file :
export module People {
export interface PeopleRestService {
getPeopleList(page: number): Person[];
getPeople(email: string): Person;
}
export interface Person {
email: string;
firstName: string;
lastName: string;
}
export var rootUrl: string;
export var peopleRestService: PeopleRestService;
export var adapter: (httpMethod: string, path: string, getParams: Object, postParams: Object, body: any)=> void;
}
The module People contains both the definition of the DTO Person and the service PeopleRestService, it also provides 3 properties :
- rootURL : URL of the service : Should be set before usage
- peopleRESTService : An instance of the service
- adapter : An adapter for RESt service call. Set to Jquery adapter by default.
Then, in your application, you can call the service like so
/// <reference path="People.d.ts" />
import p = People;
import Person = p.Person;
import prs = p.peopleRestService;
p.rootUrl = "http://someurl/root/";
var personList : Person[] = prs.getPeopleList(1);
var onePerson : Person = prs.getPeople("[email protected]");
Don't forget to import the generated file People.js in the final HTML page.
This project is licenced under the Apache v2.0 Licence
Jackson module is inspired from the jsonSchema module