Posted in General

Spring Boot App Default Web Route Issue

By default Spring Boot is an opinionated wrapper around the Spring Framework. Many things are managed for you such as HTTP port, web context root, web server implementation, etc.

If you are using a view templating framework such as Thymeleaf, you might overlook adding it to your pom.xml file such that Maven resolves all transitive dependencies for you. If you forget, you may see the following error:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Oct 20 10:44:19 EDT 2016

There was an unexpected error (type=Internal Server Error, status=500).

Path home does not start with a "/" character
To fix this, add the thymeleaf dependency to your pom file:
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Posted in General

Mongoose and momentjs

A seemingly simple issue came up with a side project of mine. Date queries can be quite a nuisance when passing this information from a datepicker in a view, to an Angular controller, through an express route and finally to some query that grabs the data and returns it back in a promise.

There are several stackoverflow posts like this one that hint at formatting your date prior to querying for the data on the backend. In my case, I used Mongoose.

Here is what I ended up doing when I wanted to specify a “date string” in the format “MM/DD/YYYY” to the backend. The express API looked something like this:

http://someserver.com/api/billinglogs/user/55ab9e79f794762b4f2cd36f/serviceDt/02%2F20%2F2016

Where this is a query to retrieve all billing records for a particular user on a specific day.

The Mongoose call within the express middleware went something like this:

exports.getBillingLogsByUserAndDate = function(req, res) {
   var startOfDay = moment(req.params.serviceDate, 'MM/DD/YYYY')
                          .startOf('day').format('MM/DD/YYYY'),
   nextDay = moment(startOfDay, 'MM/DD/YYYY').add(1,'days')
                   .format('MM/DD/YYYY');

   console.log('startOfDay: ' + startOfDay);
   console.log('nextDay: ' + nextDay);
   BillingLog.find({
      _userId: new ObjectId(req.params.id),
      serviceDate: {
         $gte: startOfDay,
         $lt: nextDay
      }
   }).exec(function(err, billingLogs) {
      res.send(billingLogs);
   })
};

ObjectId is just the value passed into req.params.id:

55ab9e79f794762b4f2cd36f

 

The console.log statements will output something like the following:

startOfDay: 02/20/2016
 nextDay: 02/21/2016

 

 

 

 

Posted in Java

Setting up a Vagrant Java 8 Environment

Pretty simple setup thanks to Rob Murray’s Vagrant Java dev box setup.

Some of the issues I encountered with this setup were pretty easy to overcome, but I thought I would outline them here and eventually provide feedback to the author for completeness.

First of all I am using a Windows 7 host with VirtualBox 4.2.16, Vagrant 1.7.2 and git (on Windows 1.9.5.msysgit.0).

Follow the instructions within the link above. Apparently, the method of pulling the required source from git in one of the steps does not use the correct protocol so you will need to edit the following files within the “vagrant-javadev-box” directory:

.git/config

.gitmodules

Change all occurrences of git:/ and git@ with https:// because public access to GitHub repositories may only work with https.

For me Vagrant did not automatically setup/install the Java 8 JRE so I needed to do it manually.

Install the appropriate package so you can run the “add-apt-repository” command which specifies the appropriate repository where the Java 8 installation package is found.

sudo apt-get install python-software-properties

Install the repository

sudo add-apt-repository ppa:webupd8team/java

sudo apt-get update

sudo apt-get install oracle-java8-installer

sudo apt-get install oracle-java8-set-default

Posted in Debian

Cleanup rootfs under Debian VirtualBox VM

This caused me some pain when I was happily working on some Angular.js and decided to install harp via npm.

sudo npm install -g harp

Since this was installed with the “-g” option and I had been using that option for a number of other modules, it placed all of these under /root.


Filesystem Size Used Avail Use% Mounted on
rootfs 323M 190M 117M 63% /
udev 10M 0 10M 0% /dev
tmpfs 1.1G 632K 1.1G 1% /run
/dev/disk/by-uuid/9fbc56af-98dc-468f-835c-202a10fab4c5 323M 190M 117M 63% /
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 3.9G 340K 3.9G 1% /run/shm
/dev/sda9 59G 38G 19G 68% /home
/dev/sda8 368M 14M 336M 4% /tmp
/dev/sda5 8.3G 4.3G 3.6G 55% /usr
/dev/sda6 2.8G 864M 1.8G 33% /var
none 421G 114G 308G 27% /media/sf_Shared

“rootfs” quickly filled up to 100%. A quick fix to this was to just create a symlink to a new /home/rootdircaches directory and move the “/root/.npm” directory there.
Another option was to cleanup any debian packages via

apt-get clean

but moving cache directories from /root and directories from /opt seemed to quickly free up space.

Posted in Java, Spring framework

Defining HibernateExceptionTranslator

I wanted to avoid defining any Spring bean XML configuration generally for my Spring apps. Firing up Jetty with Maven, you may sometimes encounter the following:

java.lang.IllegalStateException: No persistence exception translators found in bean factory. Cannot perform exception translation. at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.

When defining your beans using annotations in Spring, simply specify a method annotated with @Bean that returns an instance of HibernateExceptionTranslator. The method name can be arbitrary.

Posted in Maven

Maven submodule plugin delegation

Changing directories when executing a plugin against a specific submodule is a waste of time. Execute the following to target a specific module and its lifecycle:

mvn -pl 'submodule' 'lifecycle'

For example, from a directory
~/myapp/root-module where pom.xml is a parent pom to simple-app in that location:

mvn -pl simple-app jetty:run