Tag Archives: birtesnow

BriteSnow on Jetty – Quick Start Guide

Being a developer on a Java JEE project, words like increased productivity, faster development time, lightweight *, were always encouraging to me. I heard somewhere about BriteSnow company and their open-source products (they host them on github, which is very nice!) so I decided to give a shot to these tools and try to find out, could I really speed up the development process of an enterprise-size project.

Note that the first part of this guide is intended to provide a summary about (Brite)Snow configuration. Take it as a reminder or a cheat sheet. More detailed scenarios including persistence will follow soon and will reference this guide as a starting point.

Setup

Snow arrives through the following maven dependency. We are going to use the latest stable version, for experimental versions see their Maven section.

<dependency>
  <groupId>com.britesnow</groupId>
  <artifactId>snow</artifactId>
  <version>2.0.0</version>
</dependency>

For the beginning create a standard servlet descriptor web.xml, in src/main/webapp/WEB-INF:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <display-name>
        SnowApplication
    </display-name>
    <servlet>
        <servlet-name>
            snowServlet
        </servlet-name>
        <servlet-class>
            com.britesnow.snow.web.SnowServlet
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>
            snowServlet
        </servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Note that the official documentation goes with servlet version 2.4 instead of 3.0.

Snow also requires a properties file snow.properties to be present in the src/main/webapp/WEB-INF directory which in my case looks like:

# The Application Guice Modules (1 or more, comma seperated)
snow.webApplicationModules=org.example.britesnowdemo.AppConfig

This may seem to be a bit tricky but it’s something similar when you configure a Jersey application through annotations. Except that in Jetty you can use the @ApplicationPath, but in this case you have to point to your application class in a separate config file (which is probably not as good as having only an annotation, but anyway..).

Beware, I made a mistake here! The documentation clearly says:

Note: The first Google Guice module defined in the snow.webApplicationModules property (here the only AppConfig ) specifies the base package from where Snow scans classes for Snow method annotations (e.g., @WebGet, @WebModelHandler, …) to do the appropriate “Web bindings.” So, make sure that the first Guice module is at the root package of your application.

First I placed the AppConfig into org.example.britesnowdemo.config (because I felt that should be OK, since I pointed to the right class in snow.webApplicationModules), sadly this made BriteSnow to answer with 404 to every request I made. So remember: your snow.webApplicationModules class should be always in the root package.

Our Snow application will be configured based on the modules present in this class:

package org.example.britesnowdemo.config;

import com.google.inject.AbstractModule;

public class AppConfig extends AbstractModule {

    @Override
    protected void configure() {
    }
}

For this example I’ll just leave this empty. You will probably have entities in your application, so take a look at the sampleBookmarks AppConfig on their github repository which shows a neat way to autodiscover all your entity classes:

That would be all regarding the configuration. Next thing to do is to create a simple web-service and finally fire some HTTP request.

Web Resources

If you’re already familiar with Jetty then you will find Snow almost identical, meaning web-method discoverage, similar method annotations, path annotations, etc.:

@WebPost("/delete-item-{id}")
public Object aRestGet(@PathVar("id")Long id){
// id == 123
}

while in JAX-SR:

@POST("/delete-item-{id}")
public Object aRestGet(@PathParam("id")Long id){
// id == 123
}

For the sake of simplicity our service will only say hi to us:

package org.example.britesnowdemo;

import com.britesnow.snow.web.param.annotation.WebParam;
import com.britesnow.snow.web.rest.annotation.WebGet;
import javax.inject.Singleton;

@Singleton
public class SnowResource {

    @WebGet("/rest/sayhello")
    public String sayHelloTo(@WebParam("name") String name) {
        return "Hello, " + name + "!";
    }
}

That’s all! Now you can deploy the project with Jersey’s maven plugin:

mvn jetty:run

and fire a HTTP request to the only existing REST resource:

http://localhost:8080/britesnowdemo/rest/sayhello?name=Snow

You can download the full project from https://www.dropbox.com/s/35zr2if2tlvehbb/britesnowdemo.zip.