Get Started - geronimo-iia/restexpress GitHub Wiki
This kickstart project illustrates how to create a simple RestExpress project. You can find all this sources under git folder: restexpress-sample/echo
We're going to write a simple "Echo" service as an Helle world.
This class make your hight level business process.
On HTTP GET:
- read method will be called by default
- this method read query string paramater "echo" and send this value in response body
Read Controller and Rest for more information.
public class EchoController {
private static final String ECHO_HEADER = "echo";
public String read(Request request, Response response) {
String echo = request.getHeader(ECHO_HEADER);
if (echo == null) {
return "Please set query-string parameter 'echo' (e.g. ?echo=value)";
}
return echo;
}
}
In order to declare route on our controller, we can write a little plugin or directly declare route using RestExpressService methods. But, with this way, extention is more simple.
public class EchoPlugin extends AbstractPlugin {
private EchoController controller;
public EchoPlugin() {
super();
}
@Override
public void initialize(RestExpress server) {
controller = new EchoController();
server.uri("/", controller).name("echo.routes");
}
@Override
public void destroy(RestExpress server) {
controller = null;
}
}
Here, with
server.uri("/", controller).name("echo.routes");
we add standard route which can be found on EchoController instance, and name it 'echo.routes'
Simple isn't it ?
But wait, how we can launch this sample ?
Firt we have to declare an entry point. RestExpressEntryPoint are dynamically loaded (with JDK 6 service features), so we didn't have to touch or extends RestExpressService classe.
This entry point register our plugin EchoPlugin, and add two message observer (one for error, and another one to see http access):
public class EchoEntryPoint implements RestExpressEntryPoint {
/**
* Build a new instance.
*/
public EchoEntryPoint() {
}
/**
* @see org.restexpress.RestExpressEntryPoint#onLoad(org.restexpress.RestExpress)
*/
@Override
public void onLoad(RestExpress restExpress) throws RuntimeException {
restExpress.register(new EchoPlugin());
restExpress.addMessageObserver(new ConsoleAccesLogMessageObserver(System.err));
restExpress.addMessageObserver(new SimpleConsoleLogMessageObserver());
}
}
At last, we can wrote our main server classe by using RestExpressLauncher:
public class Echo {
public static void main(String[] args) {
RestExpressLauncher launcher = new RestExpressLauncher();
launcher.run();
}
}
That all folks !
You can see result by calling :
Have fun !
By using a little maven plugin like this one:
<plugin>
<groupId>org.intelligents-ia</groupId>
<artifactId>keystone-plugin</artifactId>
<version>${keystone-plugin.version}</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<configuration>
<mainClass>org.restexpress.RestExpressLauncher</mainClass>
</configuration>
<goals>
<goal>custom</goal>
</goals>
</execution>
</executions>
</plugin>
This create an xxx-boot.jar artifact, that you can launch with
java -jar xxx-boot.jar
See Keystone project for more information.