Skip to main content

Posts

Book Note - Learning Java Functional Programming (Richard M Reese)

The problem to solve is how to blend the various programming styles (functional programming in this case) available in Java 8 to meet an application's need. Disclaimer : All the paragraphs/codes are direct copy from the book . This note captures the key points in the book I found particularly useful to myself. Thank the author for creating such a good book on the functional programming in Java. Functional Programming Concept First-class and high-order functions are associated with functional programming. A first-class function is a computer science term. It refers to functions that can be used anywhere a first-class entity can be used. A first-class entity includes elements such as numbers and strings. They can be used as an argument to a function, returned from a function, or assigned to a variable. High-order functions depend upon the existence of first-class functions. They are functions that either: Take a function as an argument Return a function Java 8 ha...
Recent posts

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...

Hibernate: How to Save Entity With User Specified Id

Spoiled by Hibernate, we usually let Hibernate to generate the id for the entity object, like the following: @MappedSuperclass public abstract class GuidEntity { @Id @GeneratedValue ( generator = "uuid2" ) @GenericGenerator ( name = "uuid2" , strategy = "uuid2" ) @Column ( unique = true , nullable = false ) private String id ; //getter, setter, ... } When you calls .save or .persist via Hibernate session or EntityManager, an id is generated automatically for the entity object using the specified generator by Hibernated. This is a neat feature but also means you don’t have control on what the value of the id is. What if you want Hibernate to use your specified id to persist the entity object? It requires a little twist. 1. Why do you even want to do this? Simple, imagine your application has tons of data and it support bulk data import/export via xls, csv, whatever format you like. Your customer pl...