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

Setting up WordPress on Amazon Lightsail

After an unsuccessful attempt to start blogging, I decided to give it another try.  And while my site with my 4 blog entries was still up and running, I decided to find it a new home.  I have been working with AWS for awhile now and thought that Amazon Lightsail would be the perfect location.

Getting up and running was simple.  Figuring out the details took a bit more effort.  I decided to compile what I learned into a single easily accessible page. 

Setting up Amazon Lightsail Account

For $5 (as of this writing) you will get a basic instance with 512 MB of RAM, 1 vCPU and 20 GB SSD.  Head over to  https://aws.amazon.com/lightsail/ and get yourself an account. You will need a credit or a debit card to sign up but you can try it out risk free for 30 days. Once you have your account, head over to Lightsail home and create an instance.  Choose your instance location, platform (Linux) and a bluepring (WordPress).

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

7 Essential Eclipse Plugins

Every developer has a list of his or her useful Eclipse plugins. This is my list. This list is not meant to be complete and comprehensive. It is highly biased and opinionated list. There a lot of great plugins out there but this list falls under the “must have” for me personally.

1. Springsource Tool Suite (STS)

If you are using Spring in your development, STS is a must have. Ctrl+Shift+L feature (for quick search) alone is a great reason to use STS in your daily work.

2. StartExplorer Eclipse Plug-in

I found this plug-in to be great for quick access to a file outside of Eclipse. “This plug-in offers tight integration of system file manager (Windows Explorer, Gnome Nautilus, KDE Konqueror, Mac Finder, …) and shell (cmd.exe, Linux/Mac terminal) in Eclipse.”

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