Skip to content
Espen Fossen edited this page Jul 31, 2015 · 2 revisions

A great way to get introduced to Reststop is to work through this tutorial, which walks you through the creation of an Reststop based web app. The app implements a simple REST UserService with an front-end.

Create an Reststop applikasjon

Look ma, no restarts!

  • Open project in your IDE
  • Edit HelloworldResource.java and adds some politeness to the greeting: Resource should output "Hello dear world"
  • Reload the page in your browser
  • Edit HelloworldResource.java: Resource should output "Hello dear Trondheim"
  • Reload the page in your browser
  • What happened? Find the relevant test and update it
  • Reload and see the updated output
  • Add compile error to HelloWorldResource.java and reload the page. What do you see?

Looking at the Maven structure

  • plugins/helloworld/ is... a plugin!
  • plugins/pom.xml: Common build configuration for all plugins
  • webapp/pom.xml:
    • List of plugins (yours or others) to include: <plugins>..</plugins>
    • Configuration of jetty:run

Creating an service-interface

  • Define a service interface UserService.java in the API plugin:
public interface UserService {
     List<User> getAllUsers();
}
  • Define the User class:
public class User {
    private String username;
    private String fullName;
     // TODO: Add getters and setters
}

Create a plugin for implementing UserService

  • mvn reststop:create-plugin
    • name: userservice
  • userservice-plugin needs to have a <dependency> on the api plugin. Make sure the to add <scope>provided</scope>
  • Implement a mock UserServiceImpl returning a few User objects
  • In your UserServicePlugin constructor, create a new UserServiceImpl and assign it to a field
  • Export the UserService field using the @Export annotation
  • Creation of a new plugins or dependency changes currently requires a server restart, so stop the jetty instance, change to root of project and run:
    • mvn install and mvn -f webapp/pom.xml jetty:run

Create a rest-plugin which will consume UserService

  • mvn reststop:create-plugin
    • name: rest
  • Add dependency on Reststop's jaxrs-api plugin:
<dependency>
    <groupId>org.kantega.reststop</groupId>
    <artifactId>reststop-jaxrs-api</artifactId>
    <version>${reststop.version}</version>
    <scope>provided</scope>
</dependency>
  • You'll also need a dependency on the API plugin here
  • Import the UserService into RestPlugin by adding it to the RestPlugin constructor.
  • In RestPlugin.java, add a JAX-RS singleton resource which outputs the UserService's list of users on the path /users
    • Tip: See helloworld plugin's ExamplePlugin.java for how to add JAX-RS resources

Front-end

  • mvn reststop:create-plugin
    • name: ui
  • Create plugins/ui/src/main/resources/assets/users/index.html
  • Create plugins/ui/src/main/resources/assets/users/users.js
  • Goto http://localhost:8080/assets/users/index.html
  • Use your-favorite.js to fetch users and list them in HTML
    • Tips: Use Webjars to add AngularJS to your app:
<plugin>
   <groupId>org.kantega.reststop</groupId>
   <artifactId>reststop-webjars-plugin</artifactId>
   <version>${reststop.version}</version>
</plugin>
  • Then add a normal <dependency> on the AngularJS Webjar in plugins/ui/pom.xml:
<dependency>
   <groupId>org.webjars</groupId>
   <artifactId>angularjs</artifactId>
   <version>1.4.3</version>
</dependency>

Configuration

  • The rest plugin should output a maximum of N (say 10) users. Add a config property for this limit in RestPlugin.java. Annotate it with @Config and give it a defaultValue of 10.
    • Tip: See how @Config is used in ExamplePlugin
  • Verify that you can override the defaultvalue by adding a property in webapp/src/config/exampleservice.conf

Development console