Spring boot, PostgreSql setup using Gradle


Although configuring a Spring Boot application to use a persistent database from a in-memory database like H2 database might be a simple task for most developers, however since I am not developing on a frequent basis so this was a bit of a task for me, and I was not able to find a dummies guide to go about it, so following are the steps to change an in-memory database like H2 to PostgreSQL

Why PostgreSQL

Since Oracle’s acquisition of MySQL I am sceptical of using MySQL as my default Open Source database so I have started using PostGreSQL over mySQL.

Step 1:

Go to https://jdbc.postgresql.org/download.html to download the required driver for yourself. Since I installed the latest version of PostgreSQL so I download version 42.1.1 the latest version at the time of writing this blog post.

The next step is to place the PostgreSQL jdbc jar file in the project class path, also include this in your list of dependencies in eclipse, so that the compiler is able to find the JDBC driver

Step 2:

Include the dependency in the gradle build file. With my little understanding of gradle this was the most time consuming part as I could not find the correct syntax to include the PostgreSQL dependency in the gradle build file.

This is as simple as including

compile ‘postgresql:postgresql:42.1.1’

in your gradle build file.

My complete gradle.build file is as under

buildscript {
ext {
springBootVersion = ‘2.0.0.BUILD-SNAPSHOT’
}
repositories {
mavenCentral()
}
dependencies {
classpath(“org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion})
}
}

apply plugin: ‘java’
apply plugin: ‘eclipse’
apply plugin: ‘org.springframework.boot’

jar {
baseName = ‘integration’
version = ‘0.0.1-SNAPSHOT’
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
mavenCentral()
}


dependencies {
compile(‘org.springframework.boot:spring-boot-starter-data-jpa‘)
compile(‘org.springframework.boot:spring-boot-starter-web‘)
compile(‘org.springframework.boot:spring-boot-starter-web-services‘)
compile(‘org.springframework.boot:spring-boot-spring-plugin-core‘)
compile(‘org.springframework.boot:spring-boot-spring-hateoas‘)
compile ‘postgresql:postgresql:42.1.1
testCompile(‘org.springframework.boot:spring-boot-starter-test‘)
}

Step 3:

After inclusion of the dependency in the gradle.build file also include the following lines in your Application.properties file to inform the application where the PostgreSQL server is and what are the credentials to access the database

spring.datasource.url= jdbc:postgresql://localhost:5432/dbname
spring.datasource.username=postgres
spring.datasource.password=xxxxxxxx
spring.jpa.hibernate.ddl-auto=create-drop

That’s it, once you have performed the above actions, your project should now complete and use PostgreSQL as the database server, please note that I came across the following few error messages, so I thought it might be good to mention them here.

Errors:

  1. Error java.sql.SQLException: Unable to load class: org.postgresql.Driver

This just means that the compiler is not able to find the jar file for the jdbc driver. Make sure you have downloaded the jdbc driver and placed it in the project class path.

  1. Unable to execute the sql with value ‘desc’

Desc is a reserved word in PostgreSQL server so make sure that you are not using any database fields that are reserved for the specific database server.

 

Advertisements

NTLM Authentication for Java http clients


I was working to provide NTLM authentication to a RSS aggregator plugin http://confluence.atlassian.com/display/CONFEXT/RSS+aggregator+macro+plugin , in our implementation the rssaggregator needed to access rss feed from an IIS installed application within an AD intergrated setup, so our http request needed to go through NTLM authentication.

What I found out during this exercise is that if one is on the same AD domain as the server hosting the secure contents then JDK 1.5 and 1.6 does a transparent authentication at the backend, automatically transferring the login details from the http client to the server , however if the request is being made from a client outside the domain then the login details have to be provided using the username and password for the client accessing the protected resource, for that I found the Jave Authenticator class to be the best option, since you do not have to alter your existing code and just call the Authenticator before the http request and populate the authentication credentials in it. Worked really well for me.

References
Two exhaustive definitions of how ntlm works, and the interactions

http://www.innovation.ch/personal/ronald/ntlm.html
http://davenport.sourceforge.net/ntlm.html
Useful link to valid Java Implementation
http://oaklandsoftware.com/papers/ntlm.html

Java HTTP Proxy Settings


Overview

For local networks within an organization, access to the public-domain Internet is often via a HTTP Proxy. This article talks about the HTTP proxy settings for the Java environment. I did not find a good document on the Web to describe these settings; Had to discover many of them by trial-and-error. Hence this article.

Keywords

HTTP Proxy, Java Proxy Settings, Tomcat, Application Server, Servlets, HTTP Proxy Authentication for Java, Java Application Proxy Settings

Scenario

  • Your Java client runs on a machine on the Local network – Private LAN. The client could be a standalone application, or a servlet hosted on a web container like Tomcat
  • Your code access an external resource using HTTP. For example, invoking an external Web Service.
  • Your HTTP call needs to tunnel through the HTTP proxy (using SOCKS authentication). Even if authentication is not required, you would still need to configure the URL and the Port of your HTTP proxy.

Settings

Use one of the methods below for your JVM proxy settings. Try an alternate method if any particular method does not work. In most cases, you should not require any change the pre-compiled Java code for proxy settings. JVM’s environment settings should be enough to fix this problem.

Command Line JVM Settings

The proxy settings are given to the JVM via command line arguments:

 

$ java -Dhttp.proxyHost=proxyhostURL
-Dhttp.proxyPort=proxyPortNumber
-Dhttp.proxyUser=someUserName
-Dhttp.proxyPassword=somePassword javaClassToRun

Setting System Properties in Code

Add the following lines in your Java code so that JVM uses the proxy to make HTTP calls. This would, of course, require you to recompile your Java source. (The other methods do not require any recompilation.):

System.getProperties().put("http.proxyHost", "someProxyURL");
System.getProperties().put("http.proxyPort", "someProxyPort");
System.getProperties().put("http.proxyUser", "someUserName");
System.getProperties().put("http.proxyPassword", "somePassword");

Don’t hardcode the proxy settings in your source. Read these settings from a configurable text file, so your users can configure them. You might also need to set this property:

System.getProperties().put("proxySet", "true");

Or

System.getProperties().put("http.proxySet", "true");

Tomcat Settings: catalina.properties

Append these properties to the catalina.properties file in Tomcat: ${CATALINA_OME}/conf/catalina.properties file:

http.proxyHost=yourProxyURL
http.proxyPort=yourProxyPort
http.proxyUser=yourUserName
http.proxyPassword=yourPassword

Tomcat Settings: catalina.bat

Add all the parameters defined above in the ${CATALINA_HOME}/bin/catalina.bat (for Windows) or ${CATALINA_HOME}/bin/catalina.bat (for *nix):

JAVA_OPTS="-Dhttp.proxyHost=yourProxyURL ..."

(Each option is seperated by spaces.)

References