Formatting Java Time with Spring Boot using JSON

The aim of this post is to summarize and review ways of formatting Java Time objects using Spring Boot and Jackson library.

This post is organized in five steps. Each step represents one aspect of the issue and it is also related to one commit in example project repository.

Step 0 - Prerequirements

Versions and dependencies

This tutorial is based on Spring Boot version 1.3.1.RELEASE with spring-boot-starter-web. It uses jackson-datatype-jsr310 from com.fasterxml.jackson.datatype in version 2.6.4, which is a default version of Spring Boot. All of these is based on Java 8.

The Code

In the example code repository, you can find one HTTP service made with Spring Boot. This service is a GET operation, which returns a class with Java Time objects.
You can also find the integration test that deserializes the response.

Step 1 - The goal

I would like to return class Clock, containing LocalDate,LocalTime and LocalDateTime, preinitialized in constructor.

Clock - Service response class
1
2
3
4
5
6
public final class Clock {
private final LocalDate localDate;
private final LocalTime localTime;
private final LocalDateTime localDateTime;
...
}

Response class is serialized to JSON Map, which is a default behaviour. To some extent it is correct, but ISO formatted Strings in response are preferable.

LocalDate - response as JSON Map
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{  
"localDate":{
"year":2016,
"month":"JANUARY",
"era":"CE",
"dayOfYear":1,
"dayOfWeek":"FRIDAY",
"leapYear":true,
"dayOfMonth":1,
"monthValue":1,
"chronology":{
"id":"ISO",
"calendarType":"iso8601"
}
}
}

Integration testing is an appropriate way to test our functionality.

Example of integration test
1
2
3
4
5
6
ResponseEntity<Clock> resp = sut.getForEntity("http://localhost:8080/clock", Clock.class);

assertEquals(OK, resp.getStatusCode());
assertEquals(c.getLocalDate(), resp.getBody().getLocalDate());
assertEquals(c.getLocalTime(), resp.getBody().getLocalTime());
assertEquals(c.getLocalDateTime(), resp.getBody().getLocalDateTime());

Unfortunately, tests are not passing, because of deserialization problems. The exception with message is thrown can not instantiate from JSON object.

Step 2 - Adds serialization

First things first. We have to add JSR-310 module. It is a datatype module to make Jackson recognize Java 8 Date & Time API data types.

Note that in this example jackson-datatype-jsr310 version is inherited from spring-boot-dependencies dependency management.

Dependency in pom.xml
1
2
3
4
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

Response is now consistent but still, not perfect. Dates are serialized as numbers:

Dates serialized to numbers and integers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{  
"version":2,
"localDate":[
2016,
1,
1
],
"localTime":[
10,
24
],
"localDateTime":[
2016,
1,
1,
10,
24
],
"zonedDateTime":1451640240.000000000
}

We are one step closer to our goal. Tests are passing now because this format can deserialized without any additional deserializers.
How do I know?
Start an application server on commit Step 2 - Adds Object Mapper, then checkout to Step 1 - Introduce types and problems, and run integration tests without @WebIntegrationTest annotation.

Step 3 - Enables ISO formatting

ISO 8601 formatting is a standard. I’ve found it in many projects. We are going to enable and use it.
Edit spring boot properties file application.properties and add the following line:

application.properties file - disabling timestamps write
1
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false

Now, the response is something that I’ve expected:

Dates serialized as Strings
1
2
3
4
5
6
7
{  
"version":2,
"localDate":"2016-01-01",
"localTime":"10:24",
"localDateTime":"2016-01-01T10:24",
"zonedDateTime":"2016-01-01T10:24:00+01:00"
}

Step 4 - Adds on demand formatting pattern

Imagine one of your client systems does not have a capability of formatting time. It may be a primitive device, or microservice that treats this date as a collection of characters. That is why special formatting is required.

We can change formatting in response class by adding JsonFormat annotation with pattern parameter. Standard SimpleDateFormat rules apply.

Using @JsonFormat annotation
1
2
3
4
5
@JsonFormat(pattern = "dd::MM::yyyy")
private final LocalDate localDate;

@JsonFormat(pattern = "KK:mm a")
private final LocalTime localTime;

Below there is a service response using custom @JsonFormat pattern:

Custom response style
1
2
3
4
5
6
7
{  
"version":2,
"localDate":"01::01::2016",
"localTime":"10:24 AM",
"localDateTime":"2016-01-01T10:24",
"zonedDateTime":"2016-01-01T10:24:00+01:00"
}

Our tests are still passing. It means that this pattern is used for serialization in service and deserialization in tests.

Step 5 - Globally changes formatting

There are situations where you have to resign from ISO 8601 formatting in your whole application, and apply custom made standards.

In this part, we will redefine format pattern for LocalDate. This will change formatting of LocalDate in every endpoint of your API.

We have to define:

  • DateTimeFormatter with our pattern.
  • Serializer using defined pattern.
  • Deserializer using defined pattern.
  • ObjectMapper bean with custom serializer and deserializer.
  • RestTemplate that uses our ObjectMapper.

Bean ObjectMapper is defined with annotation @Primary, to override default configuration.
My custom pattern for LocalDate is dd::MM::yyyy

Object mapper bean with custom pattern
1
2
3
4
5
6
7
8
9
10
11
12
13
14

public static final DateTimeFormatter FORMATTER = ofPattern("dd::MM::yyyy");

@Bean
@Primary
public ObjectMapper serializingObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer());
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer());
objectMapper.registerModule(javaTimeModule);
return objectMapper;
}

Definitions of serializer and deserializer for all LocalDate classes:

Custom serializer and deserializer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class LocalDateSerializer extends JsonSerializer<LocalDate> {

@Override
public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(value.format(FORMATTER));
}
}

public class LocalDateDeserializer extends JsonDeserializer<LocalDate> {

@Override
public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return LocalDate.parse(p.getValueAsString(), FORMATTER);
}
}

Now, the response is formatted with our custom pattern:

Formatted response
1
2
3
{  
"localDate":"01::01::2016"
}

Tests

When we define custom serializer, our tests start to fail. It is because RestTemplate knows nothing about our deserializer. We have to create custom RestTemplateFactory that creates RestTemplate with object mapper containing our deserializer.

Custom RestTemplateFactory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Configuration
public class RestTemplateFactory {

@Autowired
private ObjectMapper objectMapper;

@Bean
public RestTemplate createRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> converters = new ArrayList<>();
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
jsonConverter.setObjectMapper(objectMapper);
converters.add(jsonConverter);
restTemplate.setMessageConverters(converters);
return restTemplate;
}
}

Conclusion

Custom formatting Dates is relatively simple, but you have to know how to set up it. Luckily, Jackson works smoothly with Spring. If you know other ways of solving this problem or you have other observations, please comment or let me know.

Photo credits: Banner, Thumbnail

GeeCon 2014

I’ve spent the last three days on GeeCon 2014. I must say I become demanding when I go to conference, so my opinion is becoming more critical.

Basically the conference was good. There was small problems with WiFi, venue wasn’t in the city center (imagine 1000 peoples ordering taxi or getting to the local bus), but generally it was ok. Most of the talks were good. There is one thing, that I am really happy about. I made a lot of new connections. This is the aspect that push me to the next edition of GeeCON :)

I really enjoyed the afterparty at ‚Stara Zajezdnia’. This place is a local brewery, so we drunk very good beer. Also we had opportunity to take a tour with experienced brewer, so I also learned new things about brewery. Thus GeeCoin game, I also meet new people ;p

There was not any feedback system, so this is my feedback. Lets make an talk about hobbies or very light topics which will take 30 minutes after the dinner. I would like to listen about how take care of you health when working all days with computer, how to make a beer, something about motorization or photography. I need a longer break after the diner with soft topic.

I’ve selected talks, that I think was good and which I would recommend for my friends (excluding keynotes):

  • All Sam Newman talks about microservices. Just must watch or read a book.
  • Josh Long talk about Spring, because live demo was exploding with energy.
  • Tom Bujok with ’33 things you want to do better’, but only the first part. Second part was to obvious for java developer with some experience.
  • Kevlin Henney with ‚Seven Ineffective Coding Habits of Many Java Programmers’ for great preparation of content.

Thats it. See you next year.

New methods in Map.Entry comparingByKey and comparingByValue

JDK 8 is still new and hot. I have explored some new methods in Map.Entry interface, to sort objects.

Image that we have following collection:

Initial data
1
2
3
4
5
Map map = new HashMap<>();
map.put("Kawasaki", 3);
map.put("Honda", 1);
map.put("Norton", 5);
map.put("Moto Guzzi", 2);

There are four new methods:

  • comparingByKey()
  • comparingByKey(Comparator<? super K> cmp)
  • comparingByValue()
  • comparingByValue(Comparator<? super K> cmp)

The method names are self-describing. If we pass different comparator, we can get different behaviour.

Comparing by value

If we want to sort by value with default behaviour (ascending), we can do something like this:

ComparingByValue
1
2
3
4
5
6
7
List<Map.Entry<String, Integer>> comparingByValue = map
.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toList());

comparingByValue.forEach(System.out::println);

As a result, we get a list sorted by values:

1
2
3
4
Honda=1
Moto Guzzi=2
Kawasaki=3
Norton=5

Comparing by key

If we want to sort by key with default behaviour (ascending), we can do something like this:

ComparingByKey
1
2
3
4
5
6
7
List<Map.Entry<String, Integer>> comparingByKey = map
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toList());

comparingByKey.forEach(System.out::println);

As a result, we get a list sorted by keys alphabetically:

1
2
3
4
Honda=1
Kawasaki=3
Moto Guzzi=2
Norton=5

Comparing by key with comparator

If we want to sort by key length, we can pass function as a comparator,  Then we can achieve something like this:

ComparingByValue with comparator
1
2
3
4
5
6
7
List<Map.Entry<String, Integer>> comparingByValue = map
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey((String s1, String s2) -> s1.length() - s2.length()))
.collect(Collectors.toList());

comparingByValue.forEach(System.out::println);

As a result, we get a list sorted by keys length:

1
2
3
4
Honda=1
Norton=5
Kawasaki=3
Moto Guzzi=2

Photo credit

Functional Programming in Java

Harnessing the Power of Java 8 Lambda Expressions

Venkat Subramaniam

My new year commitment was, that I read one book each month. I don’t predicted an unexpected events that may me slow down :( I do not give up. Next book review will be in 16’th of April.

About the book. I’ve read a beta 6 edition from 13 of January, but I think that final version is very similar to the this beta version. It is not the book about Java 8. It is a book about Lambda Expressions in JDK8. The title says that. In JDK8 there are for example changes in GC. I was missing at least one chapter or few pages about it. 

It was book easy to understand. I have Groovy and Scala experience, so there was nothing new to me. All the time I was mapping Java code to Scala code :). It was good for me. 

Next good thing is that it reads like a group of blog posts. We have some examples of imperative style, then we move to declarative or functional style. Size of chapters was appropriate for me to read before sleep. 

I didn’t like code examples (not in book, but downloadable code). Code examples was without tests and assertions. Just run and print output. I like BDD tests, and it will be great to read examples that are combined with BDD tests. 

I would recommend this book to a friend, because it is focused on good developer practices. 

Photo Credit

Replace with Regex

Ostatnio przechodziłem z HibernateSessionFactory konfigurowanego w kodzie na hibernate.cfg.xml.
Wiąże się to z zamianą mapowań, których może być w dość dużo w projekcie:

Code Configuration
1
configuration.addAnnotatedClass(pl.myproject.MyClass.class);

Każda klasa reprezentująca encje musi zostać zamieniona na coś takiego:

Code Configuration
1
<mapping class="pl.myproject.MyClass">

Najprostsze rozwiązanie jakie przychodzi to użycie zamiany z użyciem wyrażenia reguralnego. W naszych IDE należy włączyć tryb zamiany z użyciem wyrażenia reguralnego. W polu “find” definiujemy:

Code Configuration
1
configuration.addAnnotatedClass\((.*).class\);

W polu “replace” definiujemy:

Code Configuration
1
<mapping class="$1"></mapping>

Klikamy “Replace All” i zrobione. Nie ma tutaj nic trudnego. Wybranie pierwszego podzbioru za pomocą dwóch nawiasów i wstawienie tego za pomocą $1.
Proste,a nawet banalne.
Za pewne każdy edytor tekstu potrafi takie rzeczy, jednak odnoszę wrażenie, że zbyt często próbujemy robić takie rzeczy ręcznie.

Photo credit

Tomcat cluster with mod_jk sticky session session replication

Przewodnik o tym jak zrobić klaster złożony z dwóch instacji tomcata połączonego load balancerem apache mod_jk i jak uruchomić replikację sesji oraz sticky-session. System operacyjny to lubuntu 11.10.

Z góry przepraszam, za dość surowe komendy. Nie jest to tutorial krok-po-korku ani książka, żeby rozlegle wszystko opisywać. Jest to tylko ściągawka na przyszłość, lub ekstrakt z tego co trzeba zrobić.

Adres IP należy zmienić z 10.0.0.90 na localhost czy gdzie tam wasz serwerek się znajduje.

Instalacja

  • Zainstaluj:

    Instalacja
    1
    apt-get install apache2 libapache2-mod-jk2-mod-jk
  • Utworz katalog:

    Folder
    1
    /tcluster/instance1
  • Pobierz Tomcata i rozpakuj zawartość do tego katalogu.

Konfiguracja Apache mod_jk

  • Utwórz plik /etc/apache2/workers.properties:
Worker properties time
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
worker.list=loadbalancer,mystat

worker.worker1.type=ajp13
worker.worker1.host=10.0.0.90
worker.worker1.port=8881
worker.worker1.lbfactor=50
worker.worker1.cachesize=10
worker.worker1.cache_timeout=600
worker.worker1.socket_keepalive=1
worker.worker1.socket_timeout=300

worker.worker2.type=ajp13
worker.worker2.host=10.0.0.90
worker.worker2.port=8882
worker.worker2.lbfactor=50
worker.worker2.cachesize=10
worker.worker2.cache_timeout=600
worker.worker2.socket_keepalive=1
worker.worker2.socket_timeout=300

worker.loadbalancer.type=lb
worker.loadbalancer.sticky_session=true
worker.loadbalancer.balance_workers=worker1,worker2

worker.mystat.type=status

Ważna jest nazwa workera ( worker1 oraz worker2 ) adres IP oraz port.

  • Do pliku /etc/apache2/ports.conf dodaj:
    Listen port time
    1
    Listen 8585

Zmień zawartość pliku /etc/apache2/mods-enabled/jk.conf na:

Mods configuration
1
2
3
4
5
6
JKWorkersFile /etc/apache2/workers.properties
JkLogFile /var/log/apache2/mod_jk.log
JkLogLevel debug
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"

Dodaj nasłuch na porcie 8585 dla mod_jk. Zmień plik /etc/apache2/sites-enabled/000-default

1
2
3
4
<VirtualHost *:8585>
JkMount /* loadbalancer
JkMount /jkstatus mystat
</VirtualHost>

Konfiguracja instancji nr 1:

Zmień plik /tcluster/instance1/conf/server.xml tak aby przypominał:

Konfiguracja Tomcata 1
1
2
3
4
5
6
7
8
...
<Connector port="8871" protocol="HTTP/1.1" />
...
<Connector port="8881" protocol="AJP/1.3" redirectPort="8443" />
....
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker1">
....
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

Connectora się odkomentowywuje, jvmRoute trzeba dopisać, pole cluster się odkomentowywuje.

  • Ściągnij z PSI Probe i umieść go w katalogu webapps. Jest to aplikacja która może się później przydać.

  • Dodaj użytkowników do tomcata. Zedytuj plik /tcluster/instance1/conf/tomcat-users.xml:

    Tomcat users
    1
    2
    3
    4
    5
    <tomcat-users>
    ...
    <role rolename="manager"/>
    <user username="admin" password="jakieshaslo" roles="manager"/>
    </tomcat-users>
  • Przejdź do katalogu: /tcluster/instance1/webapps.
    Zmodyfikuj _examples/WEB-INF/web.xml_ dodając distributable:

    Distributable
    1
    2
    3
    4
    5
    <tomcat-users>
    ...
    <role rolename="manager"/>
    <user username="admin" password="jakieshaslo" roles="manager"/>
    </tomcat-users>

Konfiguracja instancji nr 2:

  • Skopiuj zawartość pliku /tcluster/instance1 do /tcluster/instance2
  • Zmień plik /tcluster/instance2/conf/server.xml tak aby zmienić:
Konfiguracja Tomcat 2
1
2
3
4
5
6
7
<Server port="8006" shutdown="SHUTDOWN">
...
<Connector port="8872" protocol="HTTP/1.1" />
...
<Connector port="8882" protocol="AJP/1.3" redirectPort="8443" />
....
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker2">

Testowanie

Uruchamiamy wszystko:

Startup
1
2
3
/etc/init.d/apache start
/tcluster/instance1/bin/startup.sh
/tcluster/instance2/bin/startup.sh

Testy przeprowadzić najlepiej za pomocą aplikacji z przykładów:

W moim przypadku jest ona dostępna pod adresem: http://10.0.0.90:8871/examples/servlets/servlet/SessionExample

Mamy trzy porty HTTP:

  • 8871 - instancja nr 1
  • 8872 - instancja nr 2
  • 8585 - load balancer

Po dodaniu jakiegoś aktrybutu sesji i zmianie portu z 8871 na 8872 powinniśmy mieć ten sam SessionID (Sticky-session) oraz te same atrybuty(Replikacja sesji).

Dodatkowa analiza:

  • Panel z informacja o mod_jk dostępny pod adresem http://10.0.0.90:8585/jkstatus
  • Probe (narzędzie do monitorowania) dostępne na danej instancji: http://10.0.0.90:8871/probe oraz http://10.0.0.90:8872/probe

Photo credit