Display deployed version on your maven powered Spring application

Sometimes it is useful to have an endpoint that will return your application’s maven build properties.

To set that up, there are only a few steps that need to be followed.

First, define the resource directory.  By default, Maven will look in src/main/resources. Regardless, it is a good idea to add the resources and set filtering to true.

You will need to include Maven resources plugin. Using @ helps with avoiding collisions with Spring placeholders.

Continue reading

Check whether you have multiple ContextLoader* definitions in your web.xml!

If you are using Spring in your web project and just added Spring Security, you may come across this error message when trying to start the application.

Continue reading

Spring’s @Transactional Important Points

Spring has a great way to handle transactions using @Transactional annotation. However, there are a few gotchas that one needs to watch out for. Without a good understanding of how Spring treats transactions, you can run into some unexpected and undesirable behavior. For an in depth study of the topic refer to the Sping Spring Documentation on transaction management. I will try to summarize a few important points.

1. Spring provides support for transactions using AOP Proxies

Spring AOP Proxy http://docs.spring.io/spring/docs/4.0.2.RELEASE/spring-framework-reference/htmlsingle/#tx-decl-explained

It means that you are not calling a method directly but through a proxy. This is a very important point to remember when defining transaction boundaries. In an event where a service method calls another method in the same class with a different propagation setting (PROPAGATION_REQUIRES_NEW for example), that setting will be ignored since the call is made internally. If you are not aware of the AOP concept the behavior of those methods will most likely not match your expectations.

In order to let Spring manage transactions you have to add @EnableTransactionManagement to your Java config class or if using XML configuration add <tx:annotation-driven/> . (Don’t forget the namespace: xmlns:tx="http://www.springframework.org/schema/tx"

Continue reading

Converting JSON object to a generic class with Jackson

Marshaling a Java Object to JSON is very straight forward. Here is a simple example that prints out the object’s JSON representation.

Converting a JSON String to a Java Object is similarly simple.

Sometimes, however, it is necessary to unmarshal a JSON object into a generic class. To do that we need to use TypeReference.class.

To convert JSON to a more complicated object, for example a list List<MyObject> we would need to construct a collection type. API Doc JavaType.class

For more information refer to the official Jackson Main Page.

@TestExecutionListeners is not present for class

As I was writing and running some integration tests I came across this error message.

Turns out I did not have @Test annotation on a method I was trying to run as a JUnit test. The interesting thing I only got that error when other methods in that test class were annotated with @Test. If there were no other methods it would throw a "No runnable methods exception". Completely trivial but I did not immediately connect the error message to the actual cause.

TestExecutionListener API Doc