Friday, May 12, 2017

custom deserialize your field in jackson

Currently I'm doing an integration to a third party service api. I had a trouble while trying to deserialize a field as a Date. Their json contains a date field with an odd formatting. e.g. /Date(1494579066000)/
So I have to deserialize it into a date by taking the number between paranthesis, then casting it to a Date object.
Here is my model for json

public class Result implements Serializable
{
    @JsonProperty("InspectionDate")
    @JsonDeserialize(using = MyCustomDeserializer.class, as = Date.class)
    private Date inspectionDate;
...
}

As you can see I tell Jackson to use my class for deserialization.
Now it is time to write the custom deserializer.
I'm extending JsonDeserializer and overriding deserialize method.
jsonParser will give me the field value and I'm going to parse millisecond part of the text then cast it to a Date object.


public class MyCustomDeserializer extends JsonDeserializer
{
    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException
    {
        String timestampAsString = jsonParser.getText();

        if (StringUtils.isEmpty(timestampAsString))
        {
            return null;
        }

        Matcher matcher = Pattern.compile("/Date\\(([0-9]+)\\)/").matcher(timestampAsString);

        if (!matcher.find())
        {
            return null;
        }

        String millisecondAsString = matcher.group(1);

        if (StringUtils.isEmpty(millisecondAsString))
        {
            return null;
        }

        return new Date(Long.parseLong(millisecondAsString));
    }
}

Wednesday, May 3, 2017

web sessions

eloone wrote a beatiful article on web sessions and I think that's a fine reading for everyone who wants to learn more about web sessions or for those who forgot its details.

Friday, April 21, 2017

org.openqa.selenium.WebDriverException: cannot get automation extension

In our current project, we write CI tests using Selenium. But several weeks has passed since I wrote a new test and there goes a nice exception while running the old ones.

  
org.openqa.selenium.WebDriverException: unknown error: cannot get automation extension

Doesn't explain much, right?
I find the solution in upgrading my chrome driver.

Thursday, September 22, 2016

I always thought that our use for Hibernate in projects is quite straightforward as a decision and very rarely we think if this is a good one or not. "How Hibernate Almost Ruined My Career" was quite a read and I recommend it to anyone who shouts "Hibernate" if there's some database-related stuff in the project in hand.

Saturday, January 16, 2016

I was checking the actual Elasticsearch Java Api and while I created a NodeClient with the api I got lots of exceptions which is frustrating because I just did a clean install and there's nothing complicated in my pom.xml.
These are exceptions I got with the following of a huge stacktrace of course.
java.lang.ClassNotFoundException: groovy.lang.GroovyClassLoader
 at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_66]

java.lang.ClassNotFoundException: com.github.mustachejava.Mustache
 at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_66]
 

java.lang.ClassNotFoundException: org.apache.lucene.expressions.Expression
 at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_66]
 

java.lang.ClassNotFoundException: com.sun.jna.Native
 at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_66]
So I made maven generate a dependency tree, checked dependencies in elasticsearch client parent pom and added the necessary dependencies one by one by hand. Note that these are for Elasticsearch 2.1.1. For the version of your dependencies you should check org.elasticsearch.elasticsearch pom of your version of Elasticsearch.
  
 
            org.codehaus.groovy
            groovy-all
            2.4.4
        

        
            com.github.spullara.mustache.java
            compiler
            0.8.13
        

        
            org.apache.lucene
            lucene-expressions
            5.3.1
        

        
            net.java.dev.jna
            jna
            4.1.0