Spring Boot & JsonNode: How to use it and when to turn to creating Custom Deserializers

  Рет қаралды 4,747

Dan Vega

Dan Vega

Күн бұрын

Are you a Spring developer looking to up your game in handling JSON data? Look no further! This comprehensive tutorial is designed to introduce you to the power of JsonNode-a must-know tool for traversing JSON trees effortlessly. But that's not all! We'll also dive into the art of writing custom deserializers, an invaluable skill for those special cases where the default just won't cut it. Whether you're a beginner or just looking to sharpen your skills, this video has something for everyone. Learn how to navigate complex JSON structures and tailor your deserialization process to fit specific needs, all while using the Spring framework you know and love.
🔗Resources & Links mentioned in this video:
GitHub Repository: github.com/danvega/custom-des...
👋🏻Connect with me:
Website: www.danvega.dev
Twitter: / therealdanvega
Github: github.com/danvega
LinkedIn: / danvega
Newsletter: www.danvega.dev/newsletter
SUBSCRIBE TO MY CHANNEL: bit.ly/2re4GH0 ❤️

Пікірлер: 13
@Jamous999
@Jamous999 9 ай бұрын
Thank you so much for this nice way to handle json ❤❤❤
@victorh.torres2713
@victorh.torres2713 10 ай бұрын
Awesome! Thank you.
@alexandern9266
@alexandern9266 9 ай бұрын
First of all thanks for the fantastic content you make! Can you please make a short video about the staff you use (like mic, cam, ...) to make your awesome videos?
@igorputnik4216
@igorputnik4216 9 ай бұрын
Hi. What ide and plugins do you use?
@FredrikRambris
@FredrikRambris 10 ай бұрын
The linked Git repo points to a spring-boot-read-yaml project
@DanVega
@DanVega 10 ай бұрын
Thank you, fixed!
@sudipta_samanta
@sudipta_samanta 10 ай бұрын
How to handle the check if the key is present or not in the json node? If it is very nested, it would not be nice to check for null every step.
@asterixcode
@asterixcode 10 ай бұрын
Funny thing I had to come with a solution like this video at work a few weeks ago, then got problems with nulls and yes, did null checks on every step. Maybe there is a smarter way, but who knows. Example: JsonNode detailsNode = node.get("details"); if (detailNode.has("usagePublicId") && !detailNode.get("usagePublicId").isNull()) { detailAvsc.setUsagePublicId(detailNode.get("usagePublicId").asText()); }
@asterixcode
@asterixcode 10 ай бұрын
if you are using the objectMapper, then in the setup of the test "objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);" will do the trick (not entirely sure for nulls, but at least for not mapped fields, it does), but for a custom deserializer, then no idea other than checking as above if it has the field & if it's not null, for every step.
@asterixcode
@asterixcode 10 ай бұрын
just saw the comment from FredrikRambris below... maybe Optional might be an option :D
@luisdanielmesa
@luisdanielmesa 10 ай бұрын
I don't like Spring, but it seems that you want something like jsonPath...
@FredrikRambris
@FredrikRambris 10 ай бұрын
Or @Test void parseFile() throws JsonProcessingException { var tree = objectMapper.readTree(json); var posts = Optional.ofNullable(tree.get("data")) .flatMap(n -> Optional.ofNullable(n.get("allPost"))) .flatMap(n -> Optional.ofNullable(n.get("edges"))) .filter(JsonNode::isArray) .map(Iterable::spliterator) .map(s -> StreamSupport.stream(s, false)) .stream() .flatMap(s -> s) .map(n -> n.get("node")) .map(n -> { try { return objectMapper.treeToValue(n, Post.class); } catch (JsonProcessingException e) { throw new RuntimeException(e); } }) .toList(); assertThat(posts).isNotEmpty(); } Post.class is a simple record of a post. The magic is treeToValue. Use traversing functions to get to the actual object and then use standard Jackson deserialization to your objects.
She ruined my dominos! 😭 Cool train tool helps me #gadget
00:40
Go Gizmo!
Рет қаралды 58 МЛН
World’s Deadliest Obstacle Course!
28:25
MrBeast
Рет қаралды 132 МЛН
The child was abused by the clown#Short #Officer Rabbit #angel
00:55
兔子警官
Рет қаралды 12 МЛН
Deserializing JSON: GSON and Java
21:14
Brian Fraser
Рет қаралды 20 М.
Top 7 Ways to 10x Your API Performance
6:05
ByteByteGo
Рет қаралды 311 М.
10 Spring and Spring Boot Common Mistakes You Need To STOP
15:49
Amigoscode
Рет қаралды 141 М.
Spring Boot Testcontainers - Integration Testing made easy!
33:31
Python JSON Serialization/Deserialization Made Easy!
11:46
Stoched
Рет қаралды 11 М.
Java is slow and verbose (or so they say)
20:39
Dan Vega
Рет қаралды 9 М.
Hisense Official Flagship Store Hisense is the champion What is going on?
0:11
Special Effects Funny 44
Рет қаралды 2 МЛН
Неразрушаемый смартфон
1:00
Status
Рет қаралды 1,8 МЛН