Posts

Register a custom Jackson ObjectMapper using Spring Javaconfig

For a project for a client I’m working on, we are creating a JSON REST interface as a proxy for other systems. Our system collects data and transforms it to be used by the clients of our API. Most of the data we send are Strings, but for a new feature, we are migrating some of our code to Joda Money, a very convenient way of handling basic money operations.

To send our Money data back to our clients, we do a bit of conversion. For this, we use a Jackson Serializer, which looks a bit like this:

However, finding out how to register this class was a bit a hassle, and turned out to be trivial once we found out. To register the Serializer, you’ll need to have a custom ObjectMapper, like the following:

Then we need to register the ObjectMapper in the JavaConfig of our Spring application. This can be done with the following configuration:

And that’s it. Now Jackson is automatically configured to convert all Money objects to cents using the ObjectMapper. I hope this helps for people who need this in the future so that it might save some time!

Reloading Log4j Configuration in Tomcat

When you’re in production environment and you want to diagnose some problems, it’s often handy to have some more logging available. So, when you’re using Log4j, and you want to have your log4j.xml reloaded, you can use Log4jWebConfigurer to make this happen.

To use it, you need to add a listener your web.xml. Be careful to place this before the ContextLoaderListener entry. Also important is to add a log4jRefreshInterval (in milliseconds), else Log4j won’t start it’s deamon thread.

web.xml

This should reload your log4j.xml changes whenever they occur.

Getting started with Camel, Spring and Maven

Getting up and running with Camel is not so hard, once you know how to do it. It took me a little more time than I wanted despite the extensive amount of examples provides by Camel. This post should help you get an up and running example in no time!

To start, I’ll start with the pom.xml:

This setups a simple application. Next, we’ll create a Camel route:

The nice thing here is that a route is also executable, which makes testing of the route really easy. As you can see here, the application is looking into the target location, and will process everything found there by giving an Exchange to the process method of the myProcessor bean. This bean is wired in the camel-context.xml, which should be placed in the src/main/resources/META-INF/spring directory.

And the MyProcessor bean:

And that’s it. That should get you up to speed with using Camel, Spring and Maven!

Getting started with Spring, RabbitMQ and Maven on MacOS

So, you want to get started with RabbitMQ in a Maven project as soon as possible? Well, bear with me here, and we’ll get you up and running in no time!

First things first: you need to install RabbitMQ. You can do this by using MacPorts:
sudo port install rabbitmq-server

After RabbitMQ has been installed and is running, it’s time to create the project. Using the following Maven POM and standard Maven structure, we create our project:

This pom.xml includes all the dependencies needed to make the client connect with RabbitMQ

Next up, is the code to receive some messages, and to produce some messages. We’ll call them the Producer and Consumer.

and the consumer:

and we’re almost done. All we need now is the Spring applicationContext.xml, which will create the RabbitMQ queue, and wire everything together:

If you start the Producer now, the Producer will send a message to the automatically created queue (you can see this in the http://localhost:55672/#/queues. When running the Consumer, it will get the message from the RabbitMQ queue, and will convert and display it in the console. Now you’ve sent your first message on RabbitMQ!

Calling Oracle Functions using the Spring Framework

Consider you have a function like this:


function get_content_value ( p_type numberic(10,0)
, p_name varchar2
)
return clob;

And you want to call this function using Spring. One of the easiest way to do this is by using the Spring StoredProcedure. There are some small things to consider though, when using this class to call Oracle Functions.

Calling the above function can be done like this:

So, in short, the trick is to:

  • Define output parameters before the input parameters
  • Call the setFunction(true) method