Post on 13-Apr-2017
Building APIs with Kotlin and SparkBy Travis Spencer(@travisspencer)
Intro & Agenda
•Why Kotlin• Some basics of the syntax•Quick intro to Spark•Using Kotlin & Spark together
@travisspencer / @nordicapis
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
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
• fun keyword
Basic Syntax
@travisspencer / @nordicapis
• fun keyword• Return type (Unit default)
Basic Syntax
@travisspencer / @nordicapis
• fun keyword• Return type (Unit default)• Types of args after names
Basic Syntax
@travisspencer / @nordicapis
• fun keyword• Return type (Unit default)• Types of args after names
Basic Syntax
@travisspencer / @nordicapis
•Named arguments
• fun keyword• Return type (Unit default)• Types of args after names
Basic Syntax
@travisspencer / @nordicapis
•Named arguments• Single expression functions
Passing Function Literals
Call like this… …instead of this!
@travisspencer / @nordicapis
• Allows you to pass any type that inherits from Controllable• Safely upcast at run-time
Declaration-site Variance
@travisspencer / @nordicapis
• Variables must be declared as nullable •Null checks are required at compile time
Nullability Must be Explicit
@travisspencer / @nordicapis
Data Classes
• equals • hashCode • toString
• copy•Getters and setters
@travisspencer / @nordicapis
Multiple Return Values
@travisspencer / @nordicapis
Overridable Methods
@travisspencer / @nordicapis
Overriding Open Methods
•Override
@travisspencer / @nordicapis
Controller Overloads
•Override•Named arguments (again)
@travisspencer / @nordicapis
Controller Overloads
•Override•Named arguments (again)
• mapOf standard function
@travisspencer / @nordicapis
Controller Overloads
•Override•Named arguments (again)
• mapOf standard function• to keyword to nicely define associative array
@travisspencer / @nordicapis
Smartcasts
Is of type Any!
@travisspencer / @nordicapis
Smartcasts
is operator checks if type is ControllerResultat run-time
@travisspencer / @nordicapis
Smartcasts
ControllerResultproperties are available after logical AND
@travisspencer / @nordicapis
Ranges
@travisspencer / @nordicapis
String Interpolation
@travisspencer / @nordicapis
Multi-line Strings
val l = b?.length() ?: -1
Elvis Operator
Extension Functions
Adds foo() to all strings
@travisspencer / @nordicapis
And of course equality is done right!
Equals
@travisspencer / @nordicapis
• Conversion tools built into Intellij & Eclipse •Maven, Ant, Gradle•Docs with Javadoc and Markdown•…and
Tools
@travisspencer / @nordicapis
A Debugger!
@travisspencer / @nordicapis
Using Kotlin with Spark
Intro to Spark
• Tiny framework for setting up routes•No XML config•No annotations• Starts a Web server automatically• sparkjava.com
Request & Response Classes
Request & Response objects have lots of useful methods
Aborting Requests
Parameterized URL Patterns
http://localhost:4567/users/bob
bob == :name parameter defined in route
Templatized Responses
Building a Robust API with SparkControllers, Dependency Injection, etc.
Code Walkthrough
• 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