Micronaut can convert String values defined as a number followed by (case-insensitive) KB/MB/GB
to a number value in certain cases.
The conversion service in Micronaut supports the @ReadableBytes
annotation that we can apply to a method parameter.
Micronaut will then parse the String value and convert it to a number.
The value 1Kb
is converted to 1024
.
We can use this for example in a configuration class or path variable in a controller.
Continue reading →
In my early days I spent most of my time fixing bugs on a huge enterprise application, so by now I learned from experience that a lot of bugs could have been easily prevented. This is why I prefer a Functional Programming style, I love how FP handles state. As a software consultant I get to switch companies and teams quite regularly and most projects I have been working on use java 7 or 8. This almost always leads to a few dicussions regarding programming style. So today I would like to talk about good FP principles, and how Java makes them hard (and why languages like Kotlin are awesome).
Most of my variables (95%+) are usually immutable, and I would like my compiler to check this for me. In Kotlin we have val
and var
to declare variables, val
being immutable and val
being mutable. To make a variable non-mutable in Java, we need to use the final
keyword before all variables, including parameters to get the behaviour I desire.
Continue reading →
Back in '98 when I started with my CS education Java was the first programming language we were taught.
Before I had experimented with QBasic, C and assembly but that was the moment where I started to really grow into being a software engineer.
While I made something excursions to other languages; PHP, C#, JavaScript and even ColdFusion, Java always was my 'base' where I returned to.
My great love as it were.
But now she’s fallen out of fashion and I’m moving on to greener pastures: Kotlin.
You might notice quite a big gap between this post and the previous one.
There are multiple reasons for this (a project taking up a lot of my energy is one of them), but a big reason is that in my personal projects I don’t really feel like using Java anymore, but on the other hand Java is the common theme in most of my posts here.
This is also reflected in the traffic my blog is getting; the "how to do X" type posts are the most popular.
My definitive move towards Kotlin started in November last year with the (awesome, check them out!) Advent of Code contest of 2017.
I use these to challenge myself to learn new things.
Back in 2015 I used the challenge to learn Scala and last year I started using Kotlin to solve the 2017 challenges (link if you want to check them out).
While doing the 2017 challenges I was actually having so much fun with Kotlin that I started working on the 2016 challenges as well!
Continue reading →
Micronaut is reactive by nature and uses RxJava2 as implementation for the Reactive Streams API by default.
RxJava2 is on the compile classpath by default, but we can easily use Project Reactor as implementation of the Reactive Streams API.
This allows us to use the Reactor types Mono
and Flux
.
These types are also used by Spring’s Webflux framework and makes a transition from Webflux to Micronaut very easy.
How we do use Project Reactor in our Micronaut application? We only have to add the dependency the Project Reactory core library to our project.
In the following example we add it to our build.gradle
file as:
Continue reading →
Just as we are over the crest of the microservice hype and can finally see how this architectural tool might (or might not) solve our problems the next hype is already here: serverless programming!
In this first blog post I’m going to explain what serverless is, what it isn’t, and how it can change the way we create software.
In the next posts I’m going to show a few simple examples using a well known 'serverless' platform: AWS Lambda.
Originally posted here .
So what is serverless? It’s not uncommon to see developers joke about how silly the term is because there’s obviously still a server right? Of course there is.
It’s impossible to run software without a CPU somewhere.
So why the name? Well, let’s take a step back in history to how we did things a few decades ago (and how a lot of companies still work!).
Continue reading →
On my current project we use Java 8, Spring 4.3 and Tomcat 7.0 as application server.
After the scheduling functionality was added to the project the application server did not shut down any more, it hung till the end of time.
I like to use the default Java implementations when possible so the configured scheduler was the java.util.concurrent.ScheduledThreadPoolExecutor
.
After some investigation we found out that the application server did not shutdown because the created ScheduledExecutorService
bean did not stop.
The threads in the thread pool didn’t stop when the application context is destroyed.
The destroyMethod is defined and Spring calls the shutdown method but it still did not stop the application server.
When I tested with Spring Boot it shuts down without a problem. Spring has also their own ThreadPoolTaskScheduler
with much more configuration options.
Strangely it does not implement the same interface java.util.concurrent.ScheduledExecutorService
as I would expect from the Spring framework but you can use both implementations.
Continue reading →
In a previous post we saw how we can use Spring Boot in a Ratpack application.
But the integration can also be the other way around: using Ratpack in a Spring Boot application.
This way we can use Ratpack’s power to handle requests sent to our Spring Boot application and still use all Spring Boot features in our application.
The easiest way to add Ratpack to a Spring Boot application is adding a Ratpack dependency and use the @EnableRatpack
annotation.
With this annotation a RatpackServer
instance is created and started along with configuration options.
Let’s see an example Spring Boot application with Ratpack enabled.
First we add Ratpack as dependency to our Spring Boot application.
In our example we also add Ratpack’s Dropwizard module as dependency. We use Gradle in our example:
Continue reading →
In general a REST service serves JSON document formats.
In Spring Boot it is even the standard without having to configure anything.
Over HTTP we have the ability to send a Content-Type as header to instruct the server to return a certain document format (Mime type).
Besides handling JSON, XML is another common document format.
But what if we want to serve or read a YAML document format? This tutorial provides the required steps to be able to handle objects in YAML format.
To be able to send a YAML Content-Type with the header, and to be able to serialize and deserialize with the existing ObjectMappers of Jackson.
This tutorial will use Spring Boot, Jackson and a YAML dataformat extension library for Jackson.
First we need to add the YAML Jackson extension to our Gradle build file.
Continue reading →
We have a microservice architecture with one of the microservices having a Spring Batch job which processes a CSV file and calling another microservice.
Both microservices are OAuth2 protected ResourceServers.
When we call the first microservice, a batch job is started and we want the Authorization header to be passed to the second microservice.
The solution can be defined as: In a Feign RequestInterceptor, grab the current OAuth access_token and pass it on the the RequestTemplate with Hystrix running in SEMAPHORE execution isolation strategy
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableResourceServer
@EnableOAuth2Client
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication .class, args);
}
}
Continue reading →
The default JSON error body of Spring Boot can be customized or extended by defining our own ErrorAttributes
implementation.
In Spring Boot we get an error response JSON format for free.
Any thrown Exception is automatically translated to this JSON format and returned with a corresponding HTTP status.
As soon as we throw an Exception in a @RequestMapping
annotated method of a @Controller
, the thrown Exception is translated to a HTTP status and a JSON error body.
The default JSON error body looks like:
Continue reading →
In Spring we use the @EnableAutoConfiguration
each time when we use the @SpringBootApplication
annotation.
If we look at the @SpringBootApplication
we can see that this automatically enables the @EnableAutoConfiguration
.
This last mentioned annotation triggers all the auto-configuration enabled configurations on the classpath.
We can write an auto-configuration enabled @Configuration
ourself in only two steps.
package com.jdriven.example;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyOwnAutoConfiguration {
//You can define your own beans here and
//further setup this Configuration as you normally would do
}
Continue reading →
In Spring MVC we get some method argument types resolved by default and injected in Spring MVC controller methods.
Some examples are Model
, Locale
and OutputStream
.
What if we want to inject a custom argument in Spring MVC controller methods?
In this example we extract the X-Application-Version
HTTP header from the request and inject that as a method argument called version
.
Our controller class will look like the following:
@RestController
public class MyController {
@RequestMapping("/persons")
//I want the version to be automatically injected
public List getPersons(String version) {
.....
return someList;
}
}
Continue reading →