New in Spring 5: Functional Web Framework: Arjen Poutsma Pivotal
New in Spring 5: Functional Web Framework: Arjen Poutsma Pivotal
Arjen Poutsma
Pivotal
© 2017 Pivotal
About Arjen
© 2017 Pivotal
...but there are downsides
• “Magic”
• No link between data and behavior
• Reflection
• Performance
• Type Erasure
© 2017 Pivotal
Design Goals
• Functional Style
•java.util.function
•java.util.stream
• More library; less framework
• Explicit
• Customizable
• No reflection
© 2017 Pivotal
Functional Programming
(in Java 8)
© 2017 Pivotal
Immutability
•No change after construction
• Builders
• Thread-safety
• e.g. String
•toUpperCase()
•StringBuilder
© 2017 Pivotal
First-class Functions
• Functions as first-class citizens
• Functions as arguments
• Functions as return value
• Reusability
•e.g. String
•indexOf(char) vs indexOf(Predicate<Char>)
© 2017 Pivotal
Pure Functions
•Same result given same arguments
• No Limit side effects
• Application boundaries
• Testability
• Cacheability
• Parallelization
© 2017 Pivotal
© 2017 Pivotal
Demo
© 2017 Pivotal
HandlerFunction
© 2017 Pivotal
ServerRequest
• Similar to RequestEntity
• Immutable
• Headers
• Optional<MediaType> contentType()
• List<String> header(String)
• Body
• Mono<T> bodyToMono(Class<T>)
© 2017 Pivotal
ServerResponse
• Similar to ResponseEntity
• Immutable w/ Builder
• Headers
• lastModified(ZonedDateTime)
• header(String, String...)
• Body
• body(Publisher<T>, Class<T>)
© 2017 Pivotal
RouterFunction
© 2017 Pivotal
RouterFunctions
© 2017 Pivotal
RequestPredicate
Predicate<ServerRequest>
© 2017 Pivotal
RequestPredicates
•Common predicates
• path
• headers (Content-Type, Accept)
• Build (compose) your own!
© 2017 Pivotal
HandlerFilterFunction
BiFunction<Request,
HandlerFunction<? extends ServerResponse>,
Mono<? extends ServerResponse>>
© 2017 Pivotal
Running Router Functions
© 2017 Pivotal
Summary
© 2017 Pivotal
Q &A
Resources
• Sample: https://github.com/poutsma/web-function-sample
• Blog post: https://spring.io/blog/2016/09/22/new-in-spring-5-
functional-web-framework
• Kotlin: https://speakerdeck.com/sdeleuze/functional-web-
applications-with-spring-and-kotlin
• Reactor: http://projectreactor.io/learn
© 2017 Pivotal