Articles of 2013

Using Embedded Jetty With Spring MVC

  • access_time 5 years ago
  • date_range 27/11/2013
  • comment 4
  • share 11
  • label_outlineSpring, Spring MVC

Update 12/2017: Just use spring-boot-starter-jetty. For more info refer to the docs.

There could be some advantages in creating standalone web application versus packaging it as WAR and deploying it to container. Avoiding delving into debate, what I can think of is that such application could be easier to deploy/upgrade/restart, especially in situation when there are more web applications living in the container. For example, in case of Jetty, hot-deployment does not always work as expected, causing the need to restart whole thing, which in turn takes time during which applications are offline. Such app is also less prone to possible problems associated with setting up and later upgrading of the container as they come "bundled" with exact flavour they were probably integration-tested with.

Spring MVC without web.xml using WebApplicationInitializer

  • access_time 5 years ago
  • date_range 05/11/2013
  • comment 12
  • share 34
  • label_outlineSpring, Spring MVC

Update 12/2017: Thankfully this is no longer needed, really. Keeping for posterity.

Few months ago I started playing with Spring Framework, which I really enjoy. Looking for the tutorials I found some really great resources on the web as well as books. Unfortunately many of them were written for Spring 2.x, which lacks some recent features, mainly ability to configure everything by Java annotations (a.k.a. JavaConfig) rather than XML. That was something that kept me away from Spring at the first place - doing it programmatically gives much more sense of control and is more readable. So, that's what I'm trying to achieve here, I'll try to show you how to do it in more 'modern' way, showing integrations with various software packages along the way.

Setting Active Profile And Property Sources In Spring MVC

  • access_time 5 years ago
  • date_range 07/11/2013
  • comment 0
  • share 0
  • label_outlineSpring, Spring MVC

Update 12/2017: Just use Spring Boot profiles.

Often there are times when we need to access settings stored in property files depending on the environment we are in. For example database settings are likely to be different in development, testing and production environments. For this reason Spring offers a concept profile that appeared since version 3.1. That allows to selectively enable @Configuration classes for active profile thanks to @Profile annotation.

Developing RESTful Web Services Using Dropwizard Part III

Update 12/2017: I wouldn't go that way anymore. Dependency injection is sweet, but either take minimalistic Dropwizard approach of just not doing it, or just switch to a fully-blown solution like Spring.

This is the last article of Dropwizard series, that outlines how to use Google Guice as a lightweight way to add dependency injection capabilities while developing RESTful web services using Dropwizard.

Integrating AngularJS with RequireJS

Update 12/2017: Obsolete, don't just use Angular with Angular CLI.

When I first started developing with AngularJS keeping my controllers and directives in separate .js files for better maintainability I ended up including lots of .js files on my pages. This approach has led to some obvious drawbacks. For example, each of these files will have to be minified or combined before deployment to save bandwidth. There's also little control over load order and inter-dependencies between them, like AngularJS must be available before module can be created, and module must be present before one could attach controllers to it. So was looking for a clean solution to this problem, and that's where RequireJS came in.

Developing RESTful Web Services Using Dropwizard

  • access_time 6 years ago
  • date_range 29/04/2013
  • comment 12
  • share 29
  • label_outlineDropwizard, JAX-RS

Update 12/2017: While I think Dropwizard is overall a nice project I don't see benefit of using it over i.e. Spring Boot. Lean? Focused? Yes, until you reach the point when you need to actually do stuff and start adding to it. In that regard Spring is more complete solution and offers standard way to do these things.

Considering developing RESTful web services there are many options available for Java. To name a few one may consider Spring, writing them for a Java EE container with JAX-RS or even using something like Play Framework. Most of these are (heavier or thinner) integrated solutions though. When you step into one of these you buy it with the whole inventory of things they come with, not neccesserily targeted into developing RESTful web services.

Another approach would be to collect just what is needed to write such a service and glue them together in the project. The downside is, it takes usually some boring code to write integrating all of it into working solution (bootstraping, handling configuration files, etc.). And that is the reason I find Dropwizard helpful - it's more or less a collection of software for writing RESTful web services (Jetty, Jersey, Jackson), that takes boring stuff away and permits to jump ahead into writing the service.

Things To Remember - Java integer division

  • access_time 6 years ago
  • date_range 19/04/2013
  • comment 0
  • share 5
  • label_outlineJava

Another one which is counterintuitive to people programming in untyped languages while doing Java integer division, is that the result will be integer if both dividend and divisor were integers, regardless of the type of the quotient. See the following test case:

public void shouldShowHowDivisionWorks() {
    int a = 3;
    int b = 5;
    float c = a / b;
    assertEquals(0.0, c, 1e-3); // this is true, but may be unexpected
    c = a / (float) b;
    assertEquals(0.6, c, 1e-3); // this also is true
    c = (float) a / b;
    assertEquals(0.6, c, 1e-3); // this is true too

Things To Remember - NullPointerException on if with null boolean value

  • access_time 6 years ago
  • date_range 18/04/2013
  • comment 0
  • share 3
  • label_outlineJava

Update 12/2017: Could just smile at this now.

This one occurred to me today and it's caused by laziness I got used to while coding in untyped languages :) The condition in if statement should evaluate to boolean. If it's null, then NullPointerException is thrown as in the following test case.

@Test(expected = NullPointerException.class)
public void shouldThrowNullPointerException() throws Exception {
    Boolean someFlag = null;
    if (someFlag) {
        // unexpected
    } else {
        // could be expected