Skip to content

Instantly share code, notes, and snippets.

View abhi2495's full-sized avatar
🏠
Working from home

Abhinaba Chakraborty abhi2495

🏠
Working from home
View GitHub Profile

A technique to do MDC logging in reactive JAVA web application using Spring.

An example demonstrating printing of tenant id as mdc in logs.

A "reactive" example of in-memory caching of information using CacheMono

In this example, I have created a service which returns the id of a tenant for a given alias either from cache or (if not found) calls a REST API to fetch and stores it in the cache.

Use Case:

  • We are manually serializing the object
  • We want to serialize a particular type of field (eg. LocalDateTime type) in a custom way

How am I doing it

  • Modifying the ObjectMapper instance to create modules defining serializers for particular type and registering those modules in the ObjectMapper.

Use Case:

  • A third party library is responsible for serializing or deserializing the object eg. custom serialization of api response contracts in a spring webapplication. i.e the ObjectMapper cannot be accessed and configured directly
  • We want to serialize instances of only one class.

How am I doing it

  • Using @JsonSerialize at class level to register a custom serializer for instances of that class.

Use Case:

  • The object to be serialized/deserialized has a Map field whose key is another object that Jackson can't serialize or deserialize implicitly
  • A third party library is responsible for serializing or deserializing the object eg. custom serialization of api response contracts in a spring webapplication. i.e the ObjectMapper cannot be accessed and configured directly

How am I doing it

  • Using @JsonSerialize before Map field to register a custom serializer for the key.

To access the api in this example, first we have to procure the Auth Token (using one of the OAuth2 Flows) containing a scope "canGreet".

Assumption is that the Authorization Server supports OpenId Connect 1.0 specifications.

This gist describes the configuration required for Spring reactive WebClient to make a call to an OAuth2 protected resource through OAuth2.0 Client Credentials Grant Type Flow.

Assumption is that the Authorization Server supports OpenId Connect 1.0 specifications.