One of the great features of Kotlin is its interoperability with Java code. This allows you to easily call
'traditional' Java code from your Kotlin code, but it also helps you the other way around:
calling Kotlin code from Java.
Sometimes, a little extra work is needed to make some shiny Kotlin feature work with Java code. For example,
Kotlin supports default parameter values, which are not supported in Java. In this case, the @JvmOverloads
annotation
can be used to generate overloads for functions that contain parameters with default values.
This annotation does not only work on functions, but can also be applied on constructors. In this post I will explain how
to use this feature on the primary constructor, as it might be confusing where to place the annotation.
Continue reading →
Spring Security provides a lot of convenience to develop secure web applications.
However, it relies strongly on a SecurityContext
stored in a thread-local (inside the SecurityContextHolder
class).
If not mitigated, this causes issues in multi-threaded contexts. When using Kotlin Coroutines, there is an additional
abstraction layer where you don’t really know (and don’t want to know) on which thread(s) your code will be running.
Luckily, there is a relatively easy solution!
Continue reading →
When dealing with Maps in Kotlin, sometimes we’re only interested in entries for which the value is not null
.
Although the Kotlin Standard Library contains a filterValues
function that seems to be appropriate, this function
does not do any type conversions, resulting in a Map
which won’t contain null
values,
but is still a Map with values of a nullable type according to the compiler. There is a feature request
for the JetBrains team to add this functionality, but for now it has not been implemented (yet?).
Continue reading →
The Kotlin standard library contains a lot of helper functions on top of the Java standard library for our convenience.
Some of those functions help us in converting between different data types.
For example, the String.toInt()
function converts a number formatted as String
to its Int
representation.
But how do we accomplish the same with a Char
? Spoiler alert: NOT by using Char.toInt()
, but by using Char.digitToInt()
!
Continue reading →
Functions defined in Kotlin companion objects are often seen as the 'equivalent' of static methods in Java.
Although there are some similarities, there are also some caveats you should be aware of.
For example, how to use method references (or, to be pedantic: function references) to refer to functions
defined in a companion object.
Continue reading →
This September, Jorrit van der Ven and I attended Heapcon in Belgrade. At this two-day conference,
we shared our experiences with developing in Kotlin at the Port of Rotterdam in our talk: 'Kotlin is for hipsters'.
Continue reading →
With the release of Java 9, and the introduction of Project Jigsaw (the Java Platform Module System), we no longer have the need for a full-blown JRE to run our Java applications.
It is now possible to construct a stripped-down Java Runtime, containing the minimum set of required modules.
This allows us to create slim Docker containers without excess baggage.
The source code belonging to this blog post can be found at: https://github.com/rlippolis/java9-runtime-image
Our example application consists of two Java 9 modules (basically two JARs, with a module-info.java
).
We assume the concept of modules is familiar to the reader.
If not, you can learn more about it e.g. here: http://openjdk.java.net/projects/jigsaw/
Firstly, we have a backend
module consisting of a class which provides us with a String
(to keep it simple).
The backend module has no explicit dependencies on other modules (only an implicit dependency on java.base
).
Secondly, we have a frontend
module consisting of an executable main class.
This class gets the String from the backend and prints it to System.out
(again, very straightforward).
This module has an explicit dependency on backend
, and an implicit dependency on java.base
.
To see the application in action, build it with Maven (mvn clean package
), and run it from the command line:
(or use the provided run-app.sh
) The -p
option sets the module path (similar to the 'old' classpath).
The -m
option specifies the module and class to run.
Continue reading →
Securing an application is difficult.
Securing an entire application landscape is even more difficult! In this modern era of blazing fast microservices we do not want the additional complexity of having to secure it all manually.
This is where Spring Cloud Security comes in.
By combining proven technologies, it helps us achieve performant, configurable end-to-end security across multiple applications.
So what technologies are being combined? Well, a lot...
We will not mention them all here, but the foundation relies on Spring Boot and Spring Security OAuth.
OAuth, or, in our case, OAuth2 is basically an authorization delegation protocol.
To quote Wikipedia, OAuth:
[...] specifies a process for resource owners to authorize third-party access to their server resources without sharing their credentials.
Continue reading →