5 Mar

Jetty Jersey Guice and Azure Oh My!!

This project servers as an example of how to create a java rest api that uses dependency injection and is able to be deployed to Azure from Eclipse. For the source code visit my GitHub

Required Installs

Frameworks and Plugins

I tried newer versions of the above frameworks, and kept running into errors and issues. This ended up being the combination I found that got everything working correctly.

These can all be installed from the pom.xml

Project Configuration

  • Need to set the JDK as the default definition.
  • To do this in Eclipse go to Windows>preferences>java>Installed JRE’s
  • Click Add and locate the directory you installed the JDK and select that folder.
  • Check the checkbox for the JDK
  • Click OK
  • Create a new Azure Deployment project
  • Select “New Azure Deployment Project” icon
  • JDk tab of Wizard
    Wizard Step 1
  • Server tab of wizard
    Wizard Step 1
  • Applications tab of wizard
  • Add a new application and point it to your current workspace
    Wizard Step 1
  • Click Finish

This will allow you to now deploy to Azure as a classic cloud server project. All the necessary files are created for you and when you “Build Cloud Project For Azure” the Azure package(.cspkg) is created in the deploy folder which you can use to manually deploy out to. For more information visit https://azure.microsoft.com/en-us/documentation/articles/azure-toolkit-for-eclipse/

Points of Interest

  • ServletContextListener: This file is important because this is what allows the Azure worker role to spin up the Java rest api using dependency injection and server up requests/responses
  • Main: This file handles spinning up a local instance of Jetty Server so you can debug the project.
  • RegistrationsModule: This file handles all dependency injection registrations for the application
  • ApiServletModule: This is what handles the main wiring up of the Guice dependency injection container. This file is where any new resources/controller registrations would go. Another option would be to use dynamic resource loading for all classes in project, but I tend to keep things explicit for registrations.
  • Web.xml: This file needs to contain the defined filter and listener nodes as seen in the sample. This file is what is used by the Azure Jettyb instance to kick off the rest api.

Summary

I hope this helps others overcome the challenges faced when trying to find a fully working example of using all these frameworks, components and environments together.