New in Spring Framework 5.0: Functional Web Framework

23
© 2017 Pivotal New in Spring 5: Functional Web Framework Arjen Poutsma Pivotal

Transcript of New in Spring Framework 5.0: Functional Web Framework

© 2017 Pivotal

New in Spring 5:Functional Web Framework

Arjen PoutsmaPivotal

© 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

Function<ServerRequest, Mono<? extends ServerResponse>>

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

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

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

BiFunction<Request,HandlerFunction<? extends ServerResponse>,

Mono<? extends ServerResponse>>

HandlerFilterFunction

© 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