+ All Categories
Home > Technology > Building APIs with Kotlin and Spark

Building APIs with Kotlin and Spark

Date post: 13-Apr-2017
Category:
Upload: nordic-apis
View: 1,497 times
Download: 0 times
Share this document with a friend
41
Building APIs with Kotlin and Spark By Travis Spencer (@travisspencer)
Transcript
Page 1: Building APIs with Kotlin and Spark

Building APIs with Kotlin and SparkBy Travis Spencer(@travisspencer)

Page 2: Building APIs with Kotlin and Spark

Intro & Agenda

•Why Kotlin• Some basics of the syntax•Quick intro to Spark•Using Kotlin & Spark together

@travisspencer / @nordicapis

Page 3: Building APIs with Kotlin and Spark

Why Kotlin

• Compiles to JVM bytecode and JavaScript• From industry not academia•Open source & costs nothing to adopt• Can use existing Java or JavaScript frameworks• Very low learning curve

@travisspencer / @nordicapis

Page 4: Building APIs with Kotlin and Spark

Why Kotlin

• Supports OO and functional programming styles• Excellent IDE support (Intellij and Eclipse)• Perfect for Java & Android shops• Strong commercial support from

@travisspencer / @nordicapis

Page 5: Building APIs with Kotlin and Spark

• fun keyword

Basic Syntax

@travisspencer / @nordicapis

Page 6: Building APIs with Kotlin and Spark

• fun keyword• Return type (Unit default)

Basic Syntax

@travisspencer / @nordicapis

Page 7: Building APIs with Kotlin and Spark

• fun keyword• Return type (Unit default)• Types of args after names

Basic Syntax

@travisspencer / @nordicapis

Page 8: Building APIs with Kotlin and Spark

• fun keyword• Return type (Unit default)• Types of args after names

Basic Syntax

@travisspencer / @nordicapis

•Named arguments

Page 9: Building APIs with Kotlin and Spark

• fun keyword• Return type (Unit default)• Types of args after names

Basic Syntax

@travisspencer / @nordicapis

•Named arguments• Single expression functions

Page 10: Building APIs with Kotlin and Spark

Passing Function Literals

Call like this… …instead of this!

@travisspencer / @nordicapis

Page 11: Building APIs with Kotlin and Spark

• Allows you to pass any type that inherits from Controllable• Safely upcast at run-time

Declaration-site Variance

@travisspencer / @nordicapis

Page 12: Building APIs with Kotlin and Spark

• Variables must be declared as nullable •Null checks are required at compile time

Nullability Must be Explicit

@travisspencer / @nordicapis

Page 13: Building APIs with Kotlin and Spark

Data Classes

• equals • hashCode • toString

• copy•Getters and setters

@travisspencer / @nordicapis

Page 14: Building APIs with Kotlin and Spark

Multiple Return Values

@travisspencer / @nordicapis

Page 15: Building APIs with Kotlin and Spark

Overridable Methods

@travisspencer / @nordicapis

Page 16: Building APIs with Kotlin and Spark

Overriding Open Methods

•Override

@travisspencer / @nordicapis

Page 17: Building APIs with Kotlin and Spark

Controller Overloads

•Override•Named arguments (again)

@travisspencer / @nordicapis

Page 18: Building APIs with Kotlin and Spark

Controller Overloads

•Override•Named arguments (again)

• mapOf standard function

@travisspencer / @nordicapis

Page 19: Building APIs with Kotlin and Spark

Controller Overloads

•Override•Named arguments (again)

• mapOf standard function• to keyword to nicely define associative array

@travisspencer / @nordicapis

Page 20: Building APIs with Kotlin and Spark

Smartcasts

Is of type Any!

@travisspencer / @nordicapis

Page 21: Building APIs with Kotlin and Spark

Smartcasts

is operator checks if type is ControllerResultat run-time

@travisspencer / @nordicapis

Page 22: Building APIs with Kotlin and Spark

Smartcasts

ControllerResultproperties are available after logical AND

@travisspencer / @nordicapis

Page 23: Building APIs with Kotlin and Spark

Ranges

@travisspencer / @nordicapis

Page 24: Building APIs with Kotlin and Spark

String Interpolation

@travisspencer / @nordicapis

Page 25: Building APIs with Kotlin and Spark

Multi-line Strings

Page 26: Building APIs with Kotlin and Spark

val l = b?.length() ?: -1

Elvis Operator

Page 27: Building APIs with Kotlin and Spark

Extension Functions

Adds foo() to all strings

@travisspencer / @nordicapis

Page 28: Building APIs with Kotlin and Spark

And of course equality is done right!

Equals

@travisspencer / @nordicapis

Page 29: Building APIs with Kotlin and Spark

• Conversion tools built into Intellij & Eclipse •Maven, Ant, Gradle•Docs with Javadoc and Markdown•…and

Tools

@travisspencer / @nordicapis

Page 30: Building APIs with Kotlin and Spark

A Debugger!

@travisspencer / @nordicapis

Page 31: Building APIs with Kotlin and Spark

Using Kotlin with Spark

Page 32: Building APIs with Kotlin and Spark

Intro to Spark

• Tiny framework for setting up routes•No XML config•No annotations• Starts a Web server automatically• sparkjava.com

Page 33: Building APIs with Kotlin and Spark

Hello World

Then hit http://localhost:4567/hello

Page 34: Building APIs with Kotlin and Spark

Request & Response Classes

Request & Response objects have lots of useful methods

Page 35: Building APIs with Kotlin and Spark

Aborting Requests

Page 36: Building APIs with Kotlin and Spark

Parameterized URL Patterns

http://localhost:4567/users/bob

bob == :name parameter defined in route

Page 37: Building APIs with Kotlin and Spark

Templatized Responses

Page 38: Building APIs with Kotlin and Spark

Building a Robust API with SparkControllers, Dependency Injection, etc.

Page 39: Building APIs with Kotlin and Spark

Code Walkthrough

Page 40: Building APIs with Kotlin and Spark

• Intro to Spark - http://goo.gl/oPm0jV • Kotlin + Spark blog post • Part 1 - http://goo.gl/WlxKQq • Part 2 - http://goo.gl/cE0gxz

• Source code - http://goo.gl/x1CVuc

Links to More Info

Page 41: Building APIs with Kotlin and Spark

Recommended