Kotlin powered Spring Boot app consuming NBA Rest API

Ladies and gents, once again I’m sharing some sample app, this time we are building sample Spring Boot app implemented in Kotlin, talking to http://data.nba.net/ API to help us get some interesting, NBA related data. In this case we’re just listing NBA teams, but you extend this sample even further.

Creating project skeleton

The easiest way to create project skeleton is to navigate directly to Spring Boot Initializer website and download pre-configured project. In case you like kotlin – you can choose it from drop down with supported languages. If you’re using Intellij or Spring Tools Suite, you can achieve the same from within your IDE.

Application entry point

KotlinNbaApplicaiton.kt is our application entry point, it’s just a standard kotlin class containing main method and being annotated with @SpringBootApplication.

package rs.dodalovic.nba

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootApplication
class KotlinNbaApplication

fun main(args: Array<String>) {
    SpringApplication.run(KotlinNbaApplication::class.java, *args)
}

Handling request with TeamController

Continue reading

Advertisements

Microservices with Spring Boot, Netflix Zuul, Kotlin, Docker and MongoDB

Recently I’ve started playing with Kotlin programming language, and wanted to give it a try.

Kotlin gives us possibility to write quite concise code, such as:

@RestController
class UsersController(val userRepo: UserRepository) {

    @ResponseStatus(HttpStatus.OK)
    @GetMapping("/users/{userId}")
    fun getUser(@PathVariable userId: String): UserDTO {
        return userRepo.findOne(userId).toUserDTO()
    }
}

You can find and download source code from GitHub. Readme file there explains how to start & use services. docker-compose comes quite handy to start / shutdown all the services with simple commands and orchestrate them via docker-compose yaml configuration file.

I’ve built sample, small microservice, composed of some kind of simple api-gateway or proxy server, implemented using Spring Boot and Netflix Zuul library for proxying requests to downstream servers.

Continue reading

Listing system / spring environment properties

Ladies & Gents, I prototyped just a small app that could help you print out some basic environment related information by exposing a few Rest controller endpoints.

Running the app

The easiest way is to run the app using Spring boot maven plugin

$ mvn spring-boot:run \
  -Drun.jvmArguments="\
    -Dfoo=foo \
    -Dbar=bar \
    -Dbaz=baz \
    -Dserver.port=8080 \
    -agentlib:jdwp=transport=dt_socket,server=y,address=1044,suspend=n"

Here, we specify some Java properties that will be used by the application:

  • foo, bar, baz custom properties
  • server.port – which is the way to instruct Spring Boot which port to use
  • we also specify to start with remote debug capabilities (if we’d like to do some remote debugging with our IDE)

Rest controller exposing various system / spring properties

package rs.dodalovic.envprops;

import com.google.common.collect.ImmutableMap;
import lombok.AllArgsConstructor;
import lombok.val;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@AllArgsConstructor
public class EnvPropertiesController {
    private final Environment environment;

    @GetMapping("/sys/props")
    public Map<String, Object> getSysProperties() {
        val builder = ImmutableMap.builder();
        System.getProperties().stringPropertyNames().forEach(name -> {
            builder.put(name, environment.getProperty(name));
        });
        val systemProperties = builder.build();
        return ImmutableMap.of(
                "size", systemProperties.size(),
                "values", systemProperties
        );
    }

    @GetMapping("/sys/props/custom")
    public Map<String, Object> getCustomEnvProperties() {
        val builder = ImmutableMap.builder();
        builder.put("properties.key1", environment.getProperty("properties.key1"));
        builder.put("properties.key2", environment.getProperty("properties.key2"));

        builder.put("foo", environment.getProperty("foo"));
        builder.put("bar", environment.getProperty("bar"));
        builder.put("baz", environment.getProperty("baz"));

        val customProperties = builder.build();

        return ImmutableMap.of(
                "size", customProperties.size(),
                "values", customProperties
        );
    }

    @GetMapping("/sys/env")
    public Map<String, Object> get() {
        val env = System.getenv();
        return ImmutableMap.of(
                "size", env.size(),
                "values", env
        );
    }
}

Highlighted are lines used to get insights from running application environment. We are injecting Environment implementation which gives us access to environment properties, and application profiles.

application.properties

properties.key1=properties.val1
properties.key2=properties.val2

In the application.properties (main Spring Boot configuration file) we define two custom Spring environment properties: properties.key1 and properties.key2.

Listing system properties

$ curl -s 'http://localhost:8080/sys/props' | jq .

Response:

 {
   "size": 63,
   "values": {
     "java.runtime.name": "Java(TM) SE Runtime Environment",
     "sun.boot.library.path": "/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib",
     "java.vm.version": "25.121-b13"
     // ...
 }
}

Listing system environment

$ curl -s 'http://localhost:8080/sys/env' | jq .

Response:

{
  "size": 46,
  "values": {
    "M2": "/usr/local/Cellar/maven/3.5.0/libexec/bin",
    "GREP_COLOR": "1;33",
    "JENV_FORCEJDKHOME": "true"
    // ...
  }
}

 Listing custom spring properties

$ curl -s 'http://localhost:8080/sys/props/custom' | jq .

Response:

{
  "size": 5,
  "values": {
    "properties.key1": "properties.val1",
    "properties.key2": "properties.val2",
    "foo": "foo",
    "bar": "bar",
    "baz": "baz"
  }
}

Here we see merged collection of environment properties we either passed via command line or defined in application.properties.

You can checkout source code from GitHub

Hope you’ll find this useful. If so – don’t forget to subscribe to get notified of upcoming posts.

Asynchronous event publishing with Spring Boot

Ladies & gents, I’ve created sample Spring Boot app that demonstrates useful scenario of asynchronous publish / subscribe model. It could be useful in many ways. Idea is that, for example, you have some API handling some web requests, and apart from functionality it provides, it also publishes some event, so that some other thread can react upon it, with main functionality still being processed with request handling thread. Or even shorter – request handling thread will do it’s job, and publishes some event (using Java annotation), and some other background thread will subscribe to event and process it. You can use it for scenario you register user with your API, and publish user registered event, and in background you can subscribe to it by sending out e-mail to end user.

You can checkout source code from GitHub

Easiest way to start your application could be running it via spring-boot:run Maven goal:

mvn spring-boot:run

That will start our demo app on standard 8080 port.

Demo app contains two APIs, get user and update user. Those are just dummy implementations. It also has User domain model class.

You can then, using command line and curl tool, issue the get user request (I’m just doing pipe to jq tool  for nicer response rendering – you can omit that part):

curl -s 'http://localhost:8080/users/1' | jq .
{
 "id": 1,
 "age": 33,
 "name": "Some name"
}
2017-04-23 11:46:59.940 INFO 7421 --- [nio-8080-exec-1] de.odalinho.async.PublishingController : PublishingController::Thread.currentThread().getName() = http-nio-8080-exec-1
2017-04-23 11:46:59.941 INFO 7421 --- [pool-1-thread-3] d.o.a.m.UserNotificationPublisher : UserNotificationPublisher::Thread.currentThread().getName() = pool-1-thread-3
2017-04-23 11:46:59.941 INFO 7421 --- [pool-1-thread-3] d.o.a.m.UserNotificationPublisher : UserNotificationPublisher::event.getMessage() = User(id=1, age=33, name=Some name)

Continue reading

Football application using Spring boot, Thymeleaf and Spring RestTemplate

As part of my effort to adopt Spring boot I noticed that I need to learn new view technology in Java ecosystem, since Spring boot doesn’t promote JSP, which I was used to using. I’ve explored alternatives a bit, and decided to give a shot to Thymeleaf as templating engine.

I was exploring a bit what it offers, and seems that I’ll stick to it in the future – based on set of nice features it has. I am providing you a Spring boot application using thymeleaf as a view technology so that you can take a look at it and see it if it suits your needs.

Additionally, to make it non trivial application, I’ve decided to demonstrate usage of thymeleaf by building football (soccer) application that integrates with free football api. Just go ahead and register quickly for free api token that your app can use to communicate to the external service. My application communicates with 3rd party using Spring’s RestTemplate API.

After registering with API, you’ll get API key, which you need to pass it as JVM argument when starting application (see below).

In the upcoming posts I will give my best to present most important aspects of Thymeleaf itself. In a meanwhile, feel free to download application sources and take a look at it.

In order to run the application, you need to go to app’s root directory and execute maven command (make sure to exchange ABCDEF with your API key):

spring-boot:run -Dsoccerapis_token=ABCDEF

which will start embedded Tomcat container running on 8080 port. After application is started, you can access it via:

http://localhost:8080/

Application displays european soccer leagues. By choosing league, you can furhter drill down to teams in given league, and finally – by choosing particular league – you can see the players squad with player details.

You can download application sources at Github.

Stay tuned and please – don’t forget to subscribe in case you’re eager to find out what’s coming next in upcoming posts.