Skip to content

JAX RS Application

Vojtěch Habarta edited this page Dec 17, 2015 · 4 revisions

typescript-generator can automatically discover JSON classes used in JAX-RS REST service. For example from this REST resource class:

@Path("user")
public class UserResource {
    @GET
    public User getCurrentUser() {}
}

it can discover JSON class User.

Usage

To use this feature pass JAX-RS application class to typescript-generator and it will get a list of resource classes from this application. Example JAX-RS application:

public class TestApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        return new LinkedHashSet<Class<?>>(Arrays.asList(
                UserResource.class
        ));
    }
}

If you are using Maven add <classesFromJaxrsApplication> element to typescript-generator configuration for example like this:

<configuration>
    <classesFromJaxrsApplication>cz.habarta.TestApplication</classesFromJaxrsApplication>
</configuration>

Application constructor

Application class must have no-argument constructor which should not have any side-effects like communication with database, starting new threads etc.

If your application has constructor with parameters you can add second constructor (may be private) or second application (possibly with inheritance).

Application class doesn't have to derive directly from javax.ws.rs.core.Application, it can derive from some framework class like org.glassfish.jersey.server.ResourceConfig in Jersey.

Maven Guice conflict

If there is a guice version conflict between your application and typescript-generator-maven-plugin either

  1. remove guice initialization from no-argument constructor or
  2. add following dependency to the plugin.
<plugin>
    <groupId>cz.habarta.typescript-generator</groupId>
    <artifactId>typescript-generator-maven-plugin</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.sonatype.sisu</groupId>
            <artifactId>sisu-guice</artifactId>
            <version>3.2.6</version>
        </dependency>
    </dependencies>
</plugin>
Clone this wiki locally