Introduction In this workshop, we’re going to define a REST API and implement it in Java. Furthermore, we’re going to do it using the API-first methodology, which means we’re going to first define the API in a language-independent fashion (using OpenAPI) and then we’re going to implement that API in Java. Why would we do this? In the API-first methodology, the API exists independently from its implementation. There are a number of good reasons for doing this, the most important one being that the API represents a contract with its clients, which must maintain its integrity over time and therefore must be free of implementation details. In order to avoid getting stuck on a specific technology stack, it must be[…]

Wicket tester combined with Mockito is fantastic for unit testing your web application. However, developing the tests is harder than it should be because you have to refer to components by their path rather than their id. For example, to clock the OK button on a page, I need something like: tester.executeAjaxEvent(“border:form:pagetitlecontainer:ok”. “onclick”); i.e. instead of “ok”, I must refer to the button by its full path “border:form:pagetitlecontainer:ok”. This seems tedious – if I know that I only have one component on the page with the id “ok”, why can’t I just use “ok”. Wicket tester should notice that the component is not available and traverse the component hierarchy for it. If its ambiguous then it can complain, but if[…]

I’m a big fan of Apache Wicket, but there’s an urgent need for a better-organized component-ecosystem around Wicket. To write a real web application you need a framework (that’s Wicket) and visual components (that’s JQuery, YUI, Recaptcha etc) and you need the two to work together smoothly. A newcomer to Wicket should be impressed by the clean, component-oriented approach to producing the GUI for his application, but a production web application needs more than just HTML. So, inevitably, the next step is to try to find the stuff which will make your web-application look like all those other web 2.0 sites out there. However, very few of the Wicket components referenced from the Wicket home pages are “best-of-breed” and a[…]

Yesterday we had a bug caused by the following situation: a class X had a variable “user”. Class X had an inner, anonymous class extending class Y. Inside the inner class a reference was made to the “user” variable from the enclosing class X. Somebody then added a protected variable “user” to class Y. The java compiler silently updated the reference to “user” within the inner class from X.user to Y.user. This is pretty nasty and I hoped that some of the code-checking tools would warn about about it, so I checked the code with PMD, Findbugs and Checkstyle. Unfortunately, none of them flagged the issue.

If you work with anonymous classes a lot (as Wicket applications do), you sometimes get confronted with compiler/tool messages referring to anonymous (inner) classes with names like MyPage$1$1$2 (for instance in FindBugs reports). Its pretty tedious to figure out which class is meant. Maybe there’s a better way, but what I do is go to the target directory and call “javap MyPage$1$1$2.class”. This produces a decompiled version of the class which makes it pretty clear which class it is.

We had an amazingly annoying problem in a Wicket application. A specific user was continuously having problems with ajax controls on pages (search fields, auto-complete fields etc). The problems were caused by PageExpiredExceptions. We couldn’t understand why only this one user had these problems. This went on for ages, until today I found out that Wicket 1.4 sets a default limit of 5 page maps per session. This specific user typically worked with multiple browser tabs on the application and once he went over 5, some of the page maps got evicted and the ajax stuff started failing. The solution was to call “getSessionSettings().setMaxPageMaps(100)” to allow up to 100 page maps per session.

A while back I blogged about using an annotated constraint behavior to limit input in Wicket TextFields based on domain model annotations ( So if we annotate our domain models as follows: [sourcecode language=”java”] @Length(max = 40) private String firstName; [/sourcecode] that gets automatically be translated into a maxlength=40 attribute on any text field bound to firstName (we use a form visitor on a custom form class to add the behavior to all text fields in the form). That’s pretty cool, but unfortunately, it only works for text inputs because textarea doesn’t support the maxlength attribute. This really bothered me because its really important to limit the length of textareas since that’s where people try to paste in huge blocks[…]

When I started with Wicket, I remember fighting with CheckBoxMultipleChoice for hours trying to get it to work. I just used it again today and was surprised at how easy it is to use once you’re familiar with how Wicket works. So for anyone struggling with it, here’s how to do it. Say we want to display a list of Projects associated with a User and we want to do it with the CheckBoxMultipleChoice. The projects which the user currently has access to should be checked initially. Checking or unchecking boxes will add or remove access to projects. You can accomplish this with the following code: [sourcecode language=”java”] add(new CheckBoxMultipleChoice(“projects”, new Model(userProjects), allProjects)); [/sourcecode] Assuming you have a span with[…]

Its often useful to use an enum as the model for a dropdown (or radio group) in Wicket, but not immediately obvious how to handle the display of the values. Lets say I have an enum with three colors: [sourcecode language=”java”] enum Color { RED, GREEN, BLUE }; [/sourcecode] I can hook this up to a dropdown with the following code: [sourcecode language=”html”] [/sourcecode] [sourcecode language=”java”] enum Color { RED, GREEN, BLUE }; private Color color = Color.GREEN; add(new DropDownChoice(“color”, new Model(color), Arrays.asList(Color.values()))); [/sourcecode] However, that would display “RED”, “GREEN”, “BLUE”. I probably don’t want the dropdown to use the default toString() form of my enum declarations, but rather I want it to load translatable texts from property files. To[…]

We just started using Mockito for unit testing of our Wicket applications. Its a great tool, allowing us to easily mock out our service layers on a test-by-test basis. In the example below, we’re testing a page called ItemPage. It takes an id of an item to load – internally the page uses itemService.find(id) to load the item from the database. Using Mockito, we mock out the itemService.find call to return a test item. [sourcecode language=”java”] public class TestItemPage extends WicketTestcase { private static final long serialVersionUID = 1L; @Test public void TestItemPage() { when(itemService.find(Mockito.anyInt())).thenAnswer(new Answer() { @Override public Item answer(InvocationOnMock invocation) throws Throwable { final int id = (Item) invocation.getArguments()[0]; Item item = new Item(id); item.setTitle(“test”); item.setDeadline(new Date()); return[…]