│©2022 VMware, Inc.
Declarative Clients in Spring
Rossen Stoyanchev
Olga Maciaszek-Sharma
About Olga
Software Engineer in Spring Cloud team
Spring Cloud LoadBalancer, Spring Cloud OpenFeign, Spring Cloud Netflix,
Spring Cloud Contract, Spring Cloud Square, RSocket HTTP Bridge
About Rossen
Spring Framework and Spring for GraphQL teams
Spring MVC, WebFlux, WebSocket, RSocket
What is a declarative client?
Sample applications
Customer Service Verification Service
HTTP POST /verify
Customer Verification Result
Direct use of HTTP client
Client interface
Implementation
Implementation with helper methods
Client interface usage
What if we could just create an annotated
interface and have a library/ framework
translate it into actual HTTP client
requests?
Sample applications
Client interface method
calls HTTP calls
Proxy
Feign
OpenFeign/Feign
Feign
Java to HTTP client binder
● Core part of a former Spring Cloud Netflix module, passed over to the community
● Supports various HTTP clients, including Apache HTTP, Apache HC5, Ok HTTP
● Contracts - support for custom annotations and argument resolution
● Support for a broad range of encoders/ decoders, including Jackson and GSON
● Metrics, including Micrometer support
Spring Cloud OpenFeign
Spring Cloud OpenFeign
Spring MVC and Spring Cloud support for OpenFeign
● Spring MVC annotations support
● Auto-configurations for Feign-specific beans
● Spring Cloud LoadBalancer support
● Spring Cloud CircuitBreaker support
● Tracing support
A declarative/ interface client
A declarative/ interface client use
Spring Cloud OpenFeign - popularity
Highly-readable interface clients with robust integrations
● 906 GitHub stars
● 8670549 Maven downloads in 2021
● Nearly flat learning curve for Spring MVC users
Spring Cloud OpenFeign - issues
● Issues with reusing client interfaces as controllers
○ @RequestMapping at class level user issues (GH-547, GH-678)
○ CVE-2021-22044
● Maintenance issues caused by dependence on third party upstream project
● Lack of non-blocking support
Spring Cloud Square
Spring Cloud Square
Spring Boot and Spring Cloud support for Retrofit and OkHttp
● WebClient support for Retrofit interface clients
● Auto-configurations
● Load-balancing
● Tracing
● Only Retrofit annotations
● Incubator project
RetroSocket
Interface Clients
in Spring Framework 6.0
Interface clients are not new in the Spring Framework
Same proxy mechanism, different protocols…
We already had plans for RSocket Interface client
And Previous Request for an HTTP Interface Client
… deferred to Spring Cloud OpenFeign
And Previous Request for an HTTP Interface Client
Underlying mechanism is not specific to Spring Cloud
Spring Cloud just adds extensions on top
What should programming model look like, is re-using @RequestMapping
the right approach?
Opportunity to Revisit
Programming Model for an HTTP Interface Client
Define a single, concrete request
Avoid server-isms
What about @RequestMapping?
Maps 1 controller method to N requests
Any HTTP method Any URL path
Method parameters for @RequestMapping
HttpMethod
@RequestHeader
@RequestParam
@RequestBody
@RequestAttribute
@ModelAttribute
ServletRequest
…
Return Values from @RequestMapping
ResponseEntity
HttpHeaders
Body
void
Model
View
SseEmitter
…
New Method Annotation
@HttpExchange – declare, rather than map, a request
@HttpExchange Annotation Attributes
url
method
contentType
accept
Same as for @RequestMapping, but only a subset
Those that are not server specific
Method Parameters for @HttpExchange
URI
HttpMethod
@RequestHeader
@PathVariable
@RequestBody
@RequestParam
@CookieValue
Method Parameters for @HttpExchange
Return Values for @HttpExchange
Same as for @RequestMapping, but only a subset
Those that are not server specific
RSocket interface client
@RSocketExchange
@RSocketExchange parameters
@DestinationVariable
@Payload
Metadata Object, if followed by MimeType
MimeType
Spring Interface Clients Demo
Applications communicating over HTTP
Customer Service
Spring HTTP Interface Client
SC LoadBalancer
Verification Service
POST /verify
Customer Verification Result
Eureka Service Registry
Demo
Resources
● Demo code - reactive HTTP client
● Demo code - non-reactive HTTP client with load-balancing
● Demo-code - RSocket client
● Spring Cloud OpenFeign
● Spring Framework documentation for HTTP interface client
● Spring Framework documentation for RSocket interface client
● Spring Cloud Square
● RetroSocket

Declarative Clients in Spring