Jersey, Gson and DataTables

With all the backend pieces in place, I’ll add a jQuery DataTable to display the data. This only requires the creation of an html table and a little bit of javascript.

First, I pull in the prerequisite assets from Bootstrap, jQuery and jQuery DataTables (Bootstrap is, of course, completely optional).

1
2
3
4
5
6
7
8
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"/>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css"/>

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css"/>

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

Read More

Custom marshalling with Gson - part 2

My original motivation for this series of posts was the “new” 1.10 release of jQuery DataTables. This release added client-side processing with data delivery as json objects via ajax – a great match for a jersey back end. The Ajax data source (objects) requires that the data be provided with a key of “data” and a value of an array of objects. I’ll update our project to provide the UserMap in that format.

Here’s the UserMapMarshall class implementing JsonSerializer. First it creates an array of json objects from the UserMap values and then it creates a json object with a key of “data” and a value of the array. The Gson object is thread-safe, so it’s safe to retain as a static member.

Read More

Custom marshalling with Gson - part 1

Today, I’ll extend our project to add temporary storage of uploaded users in a HashMap<String,User> and allow retrieval of that HashMap as json. First, the new UserMap class. This class will be needed to register custom marshalling.

1
2
3
4
5
6
7
package com.ideoplex.tutorial;

import java.util.concurrent.ConcurrentHashMap;


public class UserMap extends ConcurrentHashMap<String,User> {
}

And three changes to the MyResource class. First, a static UserMap member to store the uploaded users. Second, a new getUserMap method to return the static member. And finally, an update to the postUser method to throw a WebApplicationException when uploading a duplicate email address and add the user to the static UserMap otherwise.

Read More

Jersey and Gson

Now that I have the project legwork out of the way, I can add Gson support to our web application. Our first step is adding gson as a project dependency. This will direct maven to include gson and its dependencies in the project.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ git diff
diff --git a/pom.xml b/pom.xml
index 0fe5b04..eedf81b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -70,6 +70,11 @@
<artifactId>jersey-media-moxy</artifactId>
</dependency>
-->
+ <dependency>
+ <groupId>com.google.code.gson</groupId>
+ <artifactId>gson</artifactId>
+ <version>2.3</version>
+ </dependency>
</dependencies>
<properties>
<jersey.version>2.12</jersey.version>

Next, I’ll pull in some code from REST with Java in practice by Lars Michael. These two classes implement the javax.ws.rs.ext.MesssageBodyWriter and javax.ws.rs.ext.MessageBodyReader interfaces necessary to marshall objects to and from json. The @Provider annotation allows JAX-RS autodiscovery to automatically find and install our classes. First, the GsonWriter class:

Read More

Maven lifecycle and plugins

Last week I used the mvn tomcat7:redeploy command to Deploy to Tomcat with maven. This command literally invokes the redeploy action of the tomcat7 plugin. Today, I’ll tie the tomcat7 plugin into the maven lifecycle.

The maven default build lifecycle has eight phases:

  • validate
  • compile
  • test
  • package
  • integration-test
  • verify
  • install
  • deploy

I’ll bind the tomcat7:redeploy action to the install lifecycle phase.

Read More

Deploy to Tomcat with maven

Previously, I used maven to generate and package a jersey webapp. Today, I’ll extend our project to deploy the war file to Tomcat. This basically amounts to configuring Tomcat to accept connections from maven and configuring maven to use a specific Tomcat instance.

For our sample project, I’ll use a Tomcat instance on Nitrous.io. If you’re also using Nitrous, then you’ll probably need to start by installing tomcat on your instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ parts install tomcat
=> Extracting archive...
=> Installing...
=> Activating...
=> Installed tomcat 7.0.53
To start the Apache Tomcat server:
$ parts start tomcat

To stop the Apache Tomcat server:
$ parts stop tomcat

Apache Tomcat config is located at:
$ /home/action/.parts/packages/tomcat/7.0.53/conf

Default webapps at:
$ /home/action/.parts/packages/tomcat/7.0.53/webapps

Default port: 8080

Read More

Getting started with Maven

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.

Ask ten developers about maven and you’ll probably get eleven opinions. The more experienced developers are apt to be on their 3rd or 4th opinion. My opinion of maven is similar to Churchill’s opinion of democracy:

democracy is the worst form of Government except for all those other forms that have been tried …

Churchill

Maven does everything – it will generate a template, download dependencies, build the project, run the tests and deploy the project. Let’s see how easy it is to build a jersey web application with maven.

Read More

AgileBits +1

Back last year after 1Password 4 for Mac was released we had an absolutely massive number of users who had emailed in with questions and it took us months to claw our way out of that. Unfortunately, you were one of the people we hadn’t been able to reply to. As an apology for that missed opportunity of ours to make you happy I just sent you a free copy of 1Password 4 for Windows and Mac

Kyle SwankAgilebits

The goal of any company is to keep their good customers loyal. It’s pretty clear that I’m a good customer (1Password, 1Password Pro, 1Password Family Upgrade, 1Password for Windows Family, 1Password 4 IOS, 1Password 4 Mac). And this is how AgileBits locks in my loyalty.

They didn’t need to gift me new licenses. But it’s nice to feel to feel appreciated.

Blogkeeping Update

For my readers in RSS-land, I’ve made some updates to the presentation here at Take the First Step.

Back to Landscape

I’ve returned to the default theme, landscape. I left because it took 27 minutes to generate the site. A little investigation led to the recent_posts widget as the guilty party – it was sorting the complete list of articles for every page. I removed the widget and generation time is under a minute.

This has also allowed me to change the format of my archive, category and tag pages from full text with pagination to date and title with no pagination. I think that it’s a lot easier skim over when you aren’t limited to 10 full text posts per page.

Theme Customization

I’ve made some changes to the underlying theme. The widgets now use theme properties to hide article counts and to change the archive list from monthly to annually. I also updated the CSS to use horizontal lists when displaying a bottom-bar rather than a sidebar.

I’m currently displaying the archive widget with annual links at the bottom of each page.

Theme in Github

Finally, I’m maintaining my theme in Github. This should make it easier for me to keep my theme up to date and allow me to make a pull request when I’m happy with my changes.

Net Neutrality +1

I’m not a believer in slippery slopes. But I’m going to make an exception today. Giving internet providers the right to sell preferential access to their users is a start of a slippery slope.

That’s why I’m joining the Internet Slowdown. Visitors to this weblog will see a symbolic “loading” icon to remind everyone what an internet without net neutrality would look like. If you allow cookies, then you will only see this once.