About Willem Cheizoo

I'm Willem Cheizoo, since 2012 I work as a Java Software Developer at JDriven. I am constantly searching for more effective ways of implementing and building good quality software. By writing blogposts I am sharing my knowledge and experience with you.

Release NPM package with git-flow

Having an NPM package in an enterprise environment and wanting to release that package using the git-flow model? Then using the node-generate-release can be very helpful.
This blog shows how to execute an integrated git flow release from your NPM package, even if your master and develop branches are protected.

Install plugin

Let’s assume we have all changes in the develop branch and we would like to create a release with all the current changes in develop. With the git-flow release the result will be that all changes will be merged into master and a tag for the release version is created with correct version. Before we can finish the release the correct version in NPM package.json needs to be set. This can all be nicely done with node-generate-release plugin.

Continue reading

PlantUML Pleasantness: Create diagrams from NPM and Typedoc

What if you have created an awesome diagram with PlantUML, and you would like to include that diagram in your documentation?


In this small tutorial we can include a generated PlantUML diagram in typedoc (a way of documenting typescript packages). Note: Graphiz needs to be installed to run this diagram generation.
First install the typedoc plugin:
npm install typedoc –saveDev
Now, create a typedoc.json in the root of your project. This file describes how typedoc should generate the documentation.
{
  “emitDecoratorMetadata”: true,
  “excludeExternals”: true,
  “experimentalDecorators”: true,
  “hideGenerator”: true,
  “ignoreCompilerErrors”: true,
  “includeDeclarations”: false,
  “mode”: “file”,
  “module”: “commonjs”,
  “moduleResolution”: “node”,
  “out”: “./build/docs”,
  “preserveConstEnums”: true,
  “stripInternal”: true,
  “suppressExcessPropertyErrors”: true,
  “suppressImplicitAnyIndexErrors”: true,
  “target”: “ES5”
}
Next install node-plantuml http://ift.tt/1EOlnfx globally. We should install this globally so we can also use the CLI as a NPM script later.
npm install -g node-plantuml
Go ahead and put the following snippet in your package.json
“scripts”: {
     “docs”: “typedoc –options typedoc.json ./src/ && puml generate my-diagram.puml -o build/docs/my-diagram.png”
}
Finally we are able to create typescript documentation with our generated PNG diagram. If we would include the image in our README as follows:
<a href=”./my-diagram.png” target=”_blank”>![Diagram](./my-diagram.png)</a>
and run the following command from the command-line:
npm run docs
Our documentation at ./build/docs our index page shows the README with our generated diagram.

Make Git files executable

You can check files with correct permissions into Git to be able to run them on the continuous integration server right after checkout. Setting the right properties in the git-index will force Git to checkout the file and set the right file permissions.

Set the core.fileMode in the git configuration to false. The means: Do not ignore the file mode.
Then update the git-index with –chmod for your file, commit and push the file to the repository.

See: git-update-index

Handling YAML format in your REST with Spring Boot

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.

Continue reading

Devoxx4Kids: Technologie is cool

Het is zaterdagochtend: terwijl de meeste developers uitslapen en genieten van hun vrije dag zijn een aantal JDriven collega’s al vroeg in de veren. Op naar Arnhem om kinderen in de leeftijd van 10 tot 14 jaar kennis te laten maken met programmeren. Een groep van 45 kinderen volgen drie verschillende lessen: programmeren in Scratch, python programmeren met Minecraft, zelf je mBot besturen.

De blog Nicole Wedler is een schittenderen samenvatting van hoe deze succesvolle dag is verlopen.

Integrate Angular in Spring Boot with Gradle

Having a Angular HTML5 single page application and a Spring Boot application, we would like to serve the complete Angular app from Spring Boot. This blog shows you a couple simple steps to get everything up and running: run NPM from Gradle, integrate the Gradle frontend build in the main build and support HTML5 mode in the ResourceHandler of Spring Boot.

Run NPM from Gradle

Create a subdirectory called frontend with the frontend code and build scripts (webpack, npm). Let’s assume our npm start and npm run watch output to the /frontend/dist/ directory.

First we need to make sure the frontend code is build when we run gradle build on our project. We can use the plugin gradle-node-plugin for this. Go ahead and create a /frontend/build.gradle file.

Now, if we run a gradle build from our frontend subdirectory:

  • node will be downloaded
  • npm install will be executed
  • npm run build will be executed

Run integrated Gradle build

To run the frontend submodule integrated from our root project, all we need to do is include a settings.gradle at the root of the project.

Go ahead and run gradle build from the root of our project and see that npm is downloaded and the expected npm tasks are run.

We need to include the distribution of the frontend build in the JAR. Thus the frontend:build task needs to be run before we process the resources of the JAR. Go ahead and add the following snippet to /build.gradle.

Support Angular HTML5 mode

Now all we need to do is create support for HTML5 mode in Angular. Angular is a single page application and subroutes of the application are default served with a ‘#’ hashtag separator. If we want to have regular paths, we can enable HTML5 mode. The problem in serving this Angular HTML5 application from Spring Boot is that the Spring Boot ResourceHandler cannot find these resources, since the real resources is the index.html with the JavaScript files. With the next code snippet we instruct Spring Boot to look for the index.html as well. This is inspired by http://stackoverflow.com/questions/24837715/spring-boot-with-angularjs-html5mode

Happy coding!

Spring Batch and Feign OAuth2 RequestInterceptor

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

Define Job execution

RequestInterceptor

Hystrix isolation strategy

Now we set the default isolation strategy of hystrix to SEMAPHORE. Otherwise we will get an error:
Method threw 'org.springframework.beans.factory.BeanCreationException' exception.... Scope 'session' is not active for the current thread;

Spicy Spring: Customize error JSON response with ErrorAttributes

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.

Default implementation

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:

Write your own ErrorAttributes implementation

What if we want extra attributes in this JSON error body?

Just define an implementation for ErrorAttributes and register it as a Spring @Component. The following example will result in a customized JSON error response body with the cause of the exception included.

Our resulting body will now look like:

Happy developing!

Spicy Spring: Creating a Fully Executable Jar

With a Spring (Boot/Cloud) application you can create a fully executable JAR, where the jar can be run from the command-line by just calling ./my-own-jar-0.0.1.jar.
My colleague Mr. Haki wrote a nice blog about creating a fully executable JAR for Grails.

Together with the famous one-liner of Josh Long in mind: “Make JAR not WAR!”, create a JAR whenever you develop a Spring Boot/Cloud application. As described in Mr. Haki’s blog, it is very easy to make our Spring application executable:

Maven configuration

Gradle configuration

You can find the description in the Spring Reference guide.

Happy Developing! And remember, “Make JAR not WAR!”