Skip to main content

Posts

Showing posts with the label REST

Production Ready Spring WebFlux WebClient For Spring Web MVC

Starting from Spring 5, AsyncRestTemplate is deprecated in favour of WebClient from spring-webflux. One good thing is that you don’t have to use reactive async WebFlux in order to use WebClient. Instead, you can still use WebClient in a synchronous blocking way in Spring Web MVC so your existing projects can continue to work. When converting one of my existing applications from AsyncRestTemplate to WebClient, I found there wasn’t sufficient documentation or examples to do a quick replacement. After a bit of searching and learning by trial and error, here are the code snippets I came out eventually. Here is the code gist Reference : https://docs.spring.io/spring/docs/5.0.0.RELEASE/spring-framework-reference/web-reactive.html#webflux-client https://www.callicoder.com/spring-5-reactive-webclient-webtestclient-examples/ Create WebClient Requirements Create one WebClient bean for each service that requires specific configurations. Config timeout, such as c...

Proper REST Status Code

Finding the proper REST response code can be painful sometimes when designing REST API, because The list of HTTP status code is (unnecessary) long (75 for now on wiki ). Too many possible status codes for a REST API make the API harder to be consumed by clients, whereas too few status codes do not provide enough granularity for errors. We all use 200 OK, but how often do we use the following? 205 Reset Content 300 Multiple Choices 419 Authentication Timeout This article discusses what is the proper code subset that are frequently used and well-understood, and still provide enough granularity of errors. Steve Marx from Dropbox has a nice article talking about “ How many HTTP status codes should your API use? ”. In short, the Twitter documents 15 status codes . Dropbox uses 8 specific status codes . For error code, it is common to provide a detailed error message or specific error code in JSON response. Before I begin, here are some nice sites about this...