Building APIs with Kotlin and Spark
-
Upload
nordic-apis -
Category
Technology
-
view
1.503 -
download
0
Transcript of Building APIs with Kotlin and Spark
![Page 1: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/1.jpg)
Building APIs with Kotlin and SparkBy Travis Spencer(@travisspencer)
![Page 2: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/5.jpg)
• fun keyword
Basic Syntax
@travisspencer / @nordicapis
![Page 6: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/6.jpg)
• fun keyword• Return type (Unit default)
Basic Syntax
@travisspencer / @nordicapis
![Page 7: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/7.jpg)
• fun keyword• Return type (Unit default)• Types of args after names
Basic Syntax
@travisspencer / @nordicapis
![Page 8: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/8.jpg)
• 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](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/9.jpg)
• 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](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/10.jpg)
Passing Function Literals
Call like this… …instead of this!
@travisspencer / @nordicapis
![Page 11: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/11.jpg)
• 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](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/12.jpg)
• 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](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/13.jpg)
Data Classes
• equals • hashCode • toString
• copy•Getters and setters
@travisspencer / @nordicapis
![Page 14: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/14.jpg)
Multiple Return Values
@travisspencer / @nordicapis
![Page 15: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/15.jpg)
Overridable Methods
@travisspencer / @nordicapis
![Page 16: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/16.jpg)
Overriding Open Methods
•Override
@travisspencer / @nordicapis
![Page 17: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/17.jpg)
Controller Overloads
•Override•Named arguments (again)
@travisspencer / @nordicapis
![Page 18: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/18.jpg)
Controller Overloads
•Override•Named arguments (again)
• mapOf standard function
@travisspencer / @nordicapis
![Page 19: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/20.jpg)
Smartcasts
Is of type Any!
@travisspencer / @nordicapis
![Page 21: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/21.jpg)
Smartcasts
is operator checks if type is ControllerResultat run-time
@travisspencer / @nordicapis
![Page 22: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/22.jpg)
Smartcasts
ControllerResultproperties are available after logical AND
@travisspencer / @nordicapis
![Page 23: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/23.jpg)
Ranges
@travisspencer / @nordicapis
![Page 24: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/24.jpg)
String Interpolation
@travisspencer / @nordicapis
![Page 25: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/25.jpg)
Multi-line Strings
![Page 26: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/26.jpg)
val l = b?.length() ?: -1
Elvis Operator
![Page 27: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/27.jpg)
Extension Functions
Adds foo() to all strings
@travisspencer / @nordicapis
![Page 28: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/28.jpg)
And of course equality is done right!
Equals
@travisspencer / @nordicapis
![Page 29: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/29.jpg)
• 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](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/30.jpg)
A Debugger!
@travisspencer / @nordicapis
![Page 31: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/31.jpg)
Using Kotlin with Spark
![Page 32: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/32.jpg)
Intro to Spark
• Tiny framework for setting up routes•No XML config•No annotations• Starts a Web server automatically• sparkjava.com
![Page 34: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/34.jpg)
Request & Response Classes
Request & Response objects have lots of useful methods
![Page 35: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/35.jpg)
Aborting Requests
![Page 36: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/36.jpg)
Parameterized URL Patterns
http://localhost:4567/users/bob
bob == :name parameter defined in route
![Page 37: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/37.jpg)
Templatized Responses
![Page 38: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/38.jpg)
Building a Robust API with SparkControllers, Dependency Injection, etc.
![Page 39: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/39.jpg)
Code Walkthrough
![Page 40: Building APIs with Kotlin and Spark](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/40.jpg)
• 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](https://reader033.fdocuments.in/reader033/viewer/2022061307/589ed41a1a28ab39498b4647/html5/thumbnails/41.jpg)