-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
☀️ add object to json util and avoid nullable responses from generic …
…handler
- Loading branch information
sergiomartins8
committed
Oct 16, 2020
1 parent
28bf5bf
commit 64141bd
Showing
3 changed files
with
60 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.company.utils.logging; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.SneakyThrows; | ||
|
||
/** | ||
* Allows whoever implements {@link AsJson} interface to be printed as a json string. | ||
* <br> | ||
* Pretty useful for logging model/POJO classes. | ||
* <br> | ||
* Example: | ||
* <p> | ||
* public class UserModel implements AsJson {} | ||
* </p> | ||
* <p> | ||
* public void printObjectAsJson() { | ||
* UserModel userModel = new UserModel(); | ||
* System.out.println(userModel.toJson()); | ||
* } | ||
* </p> | ||
*/ | ||
public interface AsJson { | ||
@SneakyThrows | ||
default String toJson() { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters