0% found this document useful (0 votes)
48 views23 pages

New in Spring 5: Functional Web Framework: Arjen Poutsma Pivotal

The document discusses new features in Spring 5 including a functional web framework. It introduces concepts like handler functions, router functions, and request predicates. Handler functions handle requests and return responses. Router functions route requests to handler functions and can be filtered. Router functions can run in a reactive web runtime.

Uploaded by

Faiçal Yahia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views23 pages

New in Spring 5: Functional Web Framework: Arjen Poutsma Pivotal

The document discusses new features in Spring 5 including a functional web framework. It introduces concepts like handler functions, router functions, and request predicates. Handler functions handle requests and return responses. Router functions route requests to handler functions and can be filtered. Router functions can run in a reactive web runtime.

Uploaded by

Faiçal Yahia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

New in Spring 5:

Functional Web Framework

Arjen Poutsma
Pivotal
© 2017 Pivotal
About Arjen

• Twenty years of experience in Enterprise Software


Development
•Joined SpringSource in 2005
• Development lead of Spring Web Services, Spring MVC, Spring
Scala
• Working on Spring 5
© 2017 Pivotal
I do like annotations...

© 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

Function<ServerRequest, Mono<? extends ServerResponse>>

© 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

Function<ServerRequest, Mono<HandlerFunction<? extends ServerResponse>>

© 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

• RouterFunction as @Bean in @EnableWebFlux class


•Convert RouterFunction to HttpHandler
• Reactor Netty
• Tomcat
• Servlet 3.1+
• Undertow
© 2017 Pivotal
Demo

© 2017 Pivotal
Summary

• Handler functions handle request by returning a response


• Router functions route to handler functions
• Router functions can be filtered by filter functions
• Router functions can be run in a reactive web runtime

© 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

You might also like