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.
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.
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:
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.
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 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.
I’ve had a pleasure to work with Piotrek Jagielski for about two weeks on Clojure project. I’ve learned a lot, but there is still a lot to know about Clojure for me. In this post I’ll write what fascinated, disappointed and astonished me about this programming language.
Clojure & InteliJ IDEA tips
Before you start your journey with Clojure:
Use Cursive plugin for InteliJ IDEA. In ‘14 Edition it was not in the standard plug-in repository (remove La Clojure plug-in and Cursive repository manually). For IDEA ‘15 it is in repository.
Colored brackets help me a lot. You can find configuration for colored brackets on Misophistful Github.
Fascinated
Syntax
For many people Clojure brackets are reasons to laugh. Jokes like that were funny at first: “How many brackets did you write today?” I have to admit, that at the beginning using brackets was not easy for me. Once I’ve realized that the brackets are just on the other side of the function name, everything was simple and I could code very fast. After few days I’ve realized that this brackets structure forces me to think more about the structure of the code. As a result the code is refactored and divided into small functions. Clojure forces you to use good programming habits.
Data structure is your code
Clojure is homoiconic, which means that the Clojure programs are represented by Clojure data structures. This means that when you are reading a Clojure code you see lists, maps, vectors. How cool is that! You only have to know few things and you can code.
Do not restart your JVM
Because Clojure code is represented as data structures, you can pass data structure (program) to running JVM. Furthermore, compiling your code to bytecode (classes, jars) may be eliminated.
For example, when you want to test something you are not obligated to start new JVM with tests. Instead you can just synchronize your working file with running REPL and run the function.
Traditional way of working with JVM is obsolete.
In the picture above, on the left you can see an editor, on the right there is running REPL.
The same way you can run tests, which is extremely fast. In our project we had ~80 tests. Executing them all took about one second.
Easy to read
Simplicity is the ultimate sophistication.
After getting familiar with this language, it was really easy to read code. Of course, I was not aware of everything what was happening under the hood, but consistency of the written program evoked sense of control.
Disapointed
Data structure is your code
When data structure is your code, you need to have some additional operators to write effective programs. You should get to know operators like ‘->>’, ‘->’, ‘let’, ‘letfn’, ‘do’, ‘if’, ‘recur’ …
Even if there is a good documentation (e.g. Let), you have to spend some time on analyzing it, and trying out examples.
As the time goes on, new operators will be developed. But it may lead to multiple Clojure dialects. I can imagine teams (in the same company) using different sets of operators, dealing with the same problems in different ways. It is not good to have too many tools. Nevertheless, this is just my suspicion.
Know what you do
I’ve written a function that rounds numbers. Despite the fact that this function was simple, I wanted to write test, because I was not sure if I had used the API in correct way. There is the test function below:
Great. There is nothing better than a good exception error. I’ve spent a lot of time trying to solve this, and solution was extremely simple. My function was defined with defn-, instead of defn. defn- means private scope and test code, could not access testing function.
Do not trust assertions
Assertions can be misleading. When tested code does not work properly and returns wrong results, error messages are like this:
Assertions problems
1 2 3 4
ERROR in math-test/math-operation-test (RT.java:528) should round using half up expected: (=8.31M result) actual: java.lang.IllegalArgumentException: Don't know how to create ISeq from: java.math.BigDecimal
I hadn’t got time to investigate it, but in my opinion it should work out of the box.
Summary
It is a matter of time, when tools will be better. Those problems will slow you down, and they are not nice to work with.
Astonished
The Clojure concurrency impressed me. Until then, I knew only standard Java synchronization model and Scala actors model. I’ve never though that concurrency problems can be solved in a different way. I will explain Clojure approach to concurrency, in details.
Normal variables
The closest Clojure’s analogy to the variables are vars, which can be created by def.
The whole idea of concurrent access variables can be written in one sentence. Refs ensures safe shared access to variables via STM, where mutation can only occur via transaction. Let me explain it step by step.
What is Refs?
Refs (reference) is a special type to hold references to your objects. As you can expect, basic things you can do with it is storing and reading values.
What is STM?
STM stands for Software Transactional Memory. STM is an alternative to lock-based synchronization system. If you like theory, please continue with Wikipedia, otherwise continue reading to see examples.
In the second line, we are creating reference. Name of this reference is amount. Current value is 10. In the third line, we are reading value of the reference called amount. Printed result is 10.
To modify the code we have to use dosync operation. By using it, we create transaction and only then the referenced value will be changed.
Complete example
The aim of the previous examples was to get familiar with the new operators and basic behavior. Below, I’ve prepared an example to illustrate bolts and nuts of STM, transactions and rollbacks.
The problem
Imagine we have two references for holding data:
source-vector containing three elements: “A”, “B” and “C”.
empty destination-vector.
Our goal is to copy the whole source vector to destination vector. Unfortunately, we can only use function which can copy elements one by one - copy-vector.
Moreover, we have three threads that will do the copy. Threads are started by the future function.
Keep in mind that this is probably not the best way to copy vectors, but it illustrates how STM works.
Below is the output of this function. We can clearly see that the result is correct. Destination vector has three elements. Between Sucessful write messages we can see that there are a lot of messages starting with Trying to write. What does it mean? The rollback and retry occurred.
Printed messageslang:Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14
(l/a06) Trying to write destination ... Trying to write source ... Trying to write destination ... Trying to write destination ... Sucessful write (A) Trying to write destination ... Trying to write destination ... Trying to write source ... Sucessful write (B A) Trying to write destination ... Trying to write source ... Sucessful write (C B A) => ("C" "B" "A")
Rollback
Each thread started to copy this vector, but only one succeed. The remaining two threads had to rollback work and try again one more time.
When Thread A (red one) wants to write variable, it notices that the value has been changed by someone else - conflict occurs. As a result, it stops the current work and tries again whole section of dosync. It will try until every write operation succeed.
Pros and cons of STM
Cons:
Everything that happens in dosync section has to be pure, without side effects. For example you can not send email to someone, because you might send 10 emails instead of one.
From performance perspective, it makes sense when you are reading a lot from Refs, but rarely writing it.
Pros:
Written code is easy to read, understand, modify.
Refs and transactions are part of standard library, so you can use it in Vanilla Java. Take a look at this blog post for more examples.
Summary
There is a lot that Java developers can gain from Clojure. They can learn how to approach the code and how to express the problem in the code. Also they can discover tools like STM.
If you like to develop your skills, you should definitely experiment with Clojure.
Software developer skill set should not be limited to hard programming skills. Also, important for of our work is communication, problem understanding, self-sufficiency and other soft skills.
It this blog post, I would like to show the positive aspects for software developer who participated in agile like conference. You will also find here a lot of information about Stretch Con, on which my experience is based on.
As a conclusion, I’ll present what outcome Stretch conference had on me.
Why I wanted to go?
There are many Agile/Lean/Leadership conferences, Thus, you do not have to choose Stretch. Look around for upcoming events, meetups or trainings. There is always something going on. But in this post I’ll only focus on Stretch.
On my regular basis I am software developer and at least half time of my job I spend on programming. My contribution in a company is not strictly related to any management role.
I wanted to go to Stretch Con, because I’ve belived that:
Everyone is a leader of himself/herself. You have to manage your time efficiently.
I can learn technical things by myself (by studying them). I did not know how to develop my soft skills – or at least I did not know how to start.
Understanding processes, dependencies and co-workers’ and clients’ motivations are crucial and they improve the quality of produced software.
Stretch was different, unlike any other conference I have attended, mainly because of the topic but also because of the fact that it forced me to think and interact.
Open spaces
Open spaces were great. Topics were shaped dynamically (voted via sli.do). It was a place where you could directly see, that other people, from different companies (different countries) have the same problems!
Regular form of open space
Those open spaces had a form of brainstorming ideas, where everyone throws an possible solution to the problem. It gave us the possibility to share and discuss ideas.
Joseph discussion panel
During the open space time, something unexpected happened. Discussion panel with Joseph Pelrine emerged. It started naturally, and eventually a lot of people accumulated around him. Gathered people asked Joseph questions, a he responded with deep explanation. Discussion was about:
transparency,
environment,
estimation,
product owner,
estimation,
ErlangC model.
And many more topics, but I was not able note everything. For such moments, it was worth going there.
Conference talks
In my opinion, on average every second talk was worth watching. I think it is a good score for single track conference. Listening to ‘leaders’, was priceless. Wide variety of subjects, helped me realize that this topic is huge.
Best talks
As usual, I would like to recommend 3 presentations which are worth seeing, but there are many more that migh interest you. Visit Ustream channel to watch them.
James Clear: The Surprising Power of Small Habits
After a great introduction, James presented detailed knowledge about the habits. He showed us techniques for shaping habits. Explained habit triggers. Finally, he also presented tricks how to sustain our habits. You can read more about habits at James Clear Page.
Tim Steigert: Don’t blame the goats. Get a Goatherd
It was a presentation, that forced reflection about me. It helped me tu understand who is a leader, what is the team or company and how all of it this fits into our world. Most importantly, how to ‘get’ a goatherd.
Joseph started his lecture with explanation of Complex systems. Then, he started discussing social self organization. Among many concepts that he presented, one particularly stuck in my mind. You have to setup for good thing to hapen naturally. Then you have to monitor them, and decide what to do more and what to stop. If you want to know how to setup things, you have to watch video.
I’ll remember this event as something positive. Here is my summary.
Pros:
Scene decoration was consistent with name of the conference. You may think it is not important, but it really helped me a lot, to put my brain into good mood.
Content of the gift bag, including book “This is Lean“. For the first time I’ve received the book, instead of a useless gadget in gift bag. Great idea!
Great venue localization. Venue itself was impressive, too.
Selected conference speakers including book authors, people who change things (e.g. John Bunch - Holacracy guru at Zappos). I’ve had a feeling that Conference program committee, made many hard decisions.
Cons:
Everyone were talking in Hungarian. It was so hard to start a discussion during breaks.
Outcome
I’ve made 464 lines of notes, from the whole conference. There were also official notes, in case I missed something. Great concept and great drawing.
I was there with Wojtek. After the conference we have spend 3 hours talking and discussing Stretch content. We’ve managed only to discuss only about few talks - there were a lot of material presented there.
On my way home, I’ve written down few action points, reflections about myself, that I will try to develop in the upcoming weeks.
Consciously shape your habits.
Turn the camera to yourself, see your actions and behaviors.
Stretch your horizon, to look for new opportunities and possibilities. It will help you with problem solving.
Do I really watch carefully?
Try not only to hear, but carefully listen to your peers.
Find better ways to communicate.
Discover Holocracy and decentralized way of running organizations.
Engagement, purpose, trust are more important than you think.
People mindset exists.
Research more about Complex systems.
In conclusion, I highly recommend attending this kind of event from time to time to every software developer. Surely, you will come out as a different person.