Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Building APIs with
Kotlin and Spark
By Travis Spencer
(@travisspencer)
Intro & Agenda
• Why Kotlin
• Some basics of the syntax
• Quick intro to Spark
• Using Kotlin & Spark together
@travisspen...
Why Kotlin
• Compiles to JVM bytecode and JavaScript
• From industry not academia
• Open source & costs nothing to adopt
•...
Why Kotlin
• Supports OO and functional programming styles
• Excellent IDE support (Intellij and Eclipse)
• Perfect for Ja...
• 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 a...
• fun keyword
• Return type (Unit
default)
• Types of args after
names
Basic Syntax
@travisspencer / @nordicapis
• Named a...
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
@travi...
• Variables must be declared as nullable
• Null checks are required at compile time
Nullability Must be Explicit
@travissp...
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 associat...
Smartcasts
Is of type Any!
@travisspencer / @nordicapis
Smartcasts
is operator
checks if type is
ControllerResult
at run-time
@travisspencer / @nordicapis
Smartcasts
ControllerResult
properties 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
@travi...
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...
Hello World
Then hit http://localhost:4567/hello
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 Spark
Controllers, 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.g...
Building APIs with Kotlin and Spark
Upcoming SlideShare
Loading in …5
×

Building APIs with Kotlin and Spark

1,869 views

Published on

This presentation explains how to use the Spark framework together with the Kotlin programming language and the JVM to build robust APIs. The presentation begins with some basics about the Kotlin language from JetBrains. It then gives a brief introduction to Spark, a Sinatra-inspired framework for setting up Web routes. It concludes with some references to three blog posts with more explanation and a GitHub repository where the sample code can be found.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Building APIs with Kotlin and Spark

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

×