SlideShare a Scribd company logo
1 of 23
Download to read offline
Action Jackson!
Effective JSON processing in Spring Boot Applications
Inspiration (1988)
Forked Version (2014)
Why This Talk?
Usage in Spring Boot
● spring-boot-starter-json
● REST controllers
● RestTemplate/WebClient
● Messaging
● State serialization in Batch, Session, …
● Various other places
Ways of using Jackson
● Typically used for (un)marshalling
○ Directly or, more often, via framework
● To/from custom Java types
○ Often Java beans or records
● Various alternatives available
Demo
Jackson Configuration
● Features
● Modules
● Annotations
● First two can be done by Boot
● Boot has its own defaults!
Spring Boot’s Jackson Configuration
● Various modules registered
○ Service loader: Java Time
○ Boot: Parameters, Boot extensions
● Disables serializing dates/durations as numbers
● Unknown properties ignored
● Use properties or callbacks to tune
Demo
Customization Callback Example
@Bean
Jackson2ObjectMapperBuilderCustomizer offsetDateTimeSerializationCustomization() {
return objectMapperBuilder -> objectMapperBuilder
.serializerByType(OffsetDateTime.class, new JsonSerializer<OffsetDateTime>() {
@Override
public void serialize(OffsetDateTime value, JsonGenerator gen,
SerializerProvider sp) throws IOException {
gen.writeString(value.truncatedTo(SECONDS)
.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
}
});
}
What Is A Field?
● Default is property-based access
● Note: getter defines field, setter is optional
● Can configure direct field access
○ With annotation or globally
● Constructor-based unmarshalling and records are
supported
○ Some caveats; esp. for records, esp. for versions < 2.15
Demo
Parameter Names Module
● Prevents need for @JsonProperty(“propName”)
● Registered by default with Spring Boot
● Does require “-parameters” javac arg!
○ Add yourself when using Gradle
● By default single-arg constructor uses “delegating
creator” (configurable, may want to disable this)
(Un)marshalling alternatives
Next to custom Java type, you can use:
● Map<String, Object>
○ Keys are fields, values are JSON values
○ String, number, List, Map for nested object
● Works both ways
● Use cases:
○ Ad-hoc HTTP request creation
○ Quick ‘n dirty response parsing
○ Wrapping custom type in top-level field
Demo
(Un)marshalling alternatives
Next to custom Java type, you can use:
● JsonNode
○ Like a JSON DOM Tree
● Use as-is, or via JsonPointer or JSON Path
● Use cases:
○ Extracting specific detail(s) from large documents
○ Especially when deeply nested or querying on
non-linear paths
Demo
Views
● Allow marshalling subset of fields
● Supported in Spring-MVC/Webflux
● Spring disables default view inclusion:
only explicitly included fields are marshalled
Demo
Best Practices
● Create your ObjectMappers using Boot’s builder
● Re-use your ObjectMappers, also in tests!
● Be careful when using records for unmarshalling
● Write tests
● Stay away from abstract types
● Stop parsing JSON using regexps!
Joris Kuipers
CTO TRIFORK AMSTERDAM
Thank you!
trifork.nl
@jkuipers, @triforkams
Additional Annotation Options
● @JsonValue
○ Use annotated property as serialized value
● @JsonRawValue
○ Use property literally on serialization
● @JsonUnwrapped
○ Like a JPA Embeddable: flatten structure in JSON
○ Doesn’t support constructor-based unmarshalling
● @JsonFormat
○ Customize numbers, dates, collections or enums
○ Configure case-insensitive properties

More Related Content

What's hot

Asynchronous processing in big system
Asynchronous processing in big systemAsynchronous processing in big system
Asynchronous processing in big systemNghia Minh
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean ArchitectureMattia Battiston
 
Spring boot
Spring bootSpring boot
Spring bootsdeeg
 
Building your First gRPC Service
Building your First gRPC ServiceBuilding your First gRPC Service
Building your First gRPC ServiceJessie Barnett
 
GraalVM Overview Compact version
GraalVM Overview Compact versionGraalVM Overview Compact version
GraalVM Overview Compact versionscalaconfjp
 
Testing with JUnit 5 and Spring - Spring I/O 2022
Testing with JUnit 5 and Spring - Spring I/O 2022Testing with JUnit 5 and Spring - Spring I/O 2022
Testing with JUnit 5 and Spring - Spring I/O 2022Sam Brannen
 
Clean architecture
Clean architectureClean architecture
Clean architectureLieven Doclo
 
Understanding MicroSERVICE Architecture with Java & Spring Boot
Understanding MicroSERVICE Architecture with Java & Spring BootUnderstanding MicroSERVICE Architecture with Java & Spring Boot
Understanding MicroSERVICE Architecture with Java & Spring BootKashif Ali Siddiqui
 
Nestjs MasterClass Slides
Nestjs MasterClass SlidesNestjs MasterClass Slides
Nestjs MasterClass SlidesNir Kaufman
 
Java 8-streams-collectors-patterns
Java 8-streams-collectors-patternsJava 8-streams-collectors-patterns
Java 8-streams-collectors-patternsJosé Paumard
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency GotchasAlex Miller
 

What's hot (20)

Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 
Asynchronous processing in big system
Asynchronous processing in big systemAsynchronous processing in big system
Asynchronous processing in big system
 
NestJS
NestJSNestJS
NestJS
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean Architecture
 
Spring boot
Spring bootSpring boot
Spring boot
 
Building your First gRPC Service
Building your First gRPC ServiceBuilding your First gRPC Service
Building your First gRPC Service
 
GraalVM Overview Compact version
GraalVM Overview Compact versionGraalVM Overview Compact version
GraalVM Overview Compact version
 
Testing with JUnit 5 and Spring - Spring I/O 2022
Testing with JUnit 5 and Spring - Spring I/O 2022Testing with JUnit 5 and Spring - Spring I/O 2022
Testing with JUnit 5 and Spring - Spring I/O 2022
 
Clean code slide
Clean code slideClean code slide
Clean code slide
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Clean code
Clean codeClean code
Clean code
 
Understanding MicroSERVICE Architecture with Java & Spring Boot
Understanding MicroSERVICE Architecture with Java & Spring BootUnderstanding MicroSERVICE Architecture with Java & Spring Boot
Understanding MicroSERVICE Architecture with Java & Spring Boot
 
Typescript ppt
Typescript pptTypescript ppt
Typescript ppt
 
Nestjs MasterClass Slides
Nestjs MasterClass SlidesNestjs MasterClass Slides
Nestjs MasterClass Slides
 
Java 8-streams-collectors-patterns
Java 8-streams-collectors-patternsJava 8-streams-collectors-patterns
Java 8-streams-collectors-patterns
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Maven et industrialisation du logiciel
Maven et industrialisation du logicielMaven et industrialisation du logiciel
Maven et industrialisation du logiciel
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
 

Similar to Action Jackson! Effective JSON processing in Spring Boot Applications

Tour de Jackson: Forgotten Features of Jackson JSON processor
Tour de Jackson: Forgotten Features of Jackson JSON processorTour de Jackson: Forgotten Features of Jackson JSON processor
Tour de Jackson: Forgotten Features of Jackson JSON processorTatu Saloranta
 
Simple Jackson with DropWizard
Simple Jackson with DropWizardSimple Jackson with DropWizard
Simple Jackson with DropWizardTatu Saloranta
 
Utopia Kindgoms scaling case: From 4 to 50K users
Utopia Kindgoms scaling case: From 4 to 50K usersUtopia Kindgoms scaling case: From 4 to 50K users
Utopia Kindgoms scaling case: From 4 to 50K usersJaime Buelta
 
Utopia Kingdoms scaling case. From 4 users to 50.000+
Utopia Kingdoms scaling case. From 4 users to 50.000+Utopia Kingdoms scaling case. From 4 users to 50.000+
Utopia Kingdoms scaling case. From 4 users to 50.000+Python Ireland
 
Node.js Course 1 of 2 - Introduction and first steps
Node.js Course 1 of 2 - Introduction and first stepsNode.js Course 1 of 2 - Introduction and first steps
Node.js Course 1 of 2 - Introduction and first stepsManuel Eusebio de Paz Carmona
 
There is Javascript in my SQL
There is Javascript in my SQLThere is Javascript in my SQL
There is Javascript in my SQLPGConf APAC
 
Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type
Postgrtesql as a NoSQL Document Store - The JSON/JSONB data typePostgrtesql as a NoSQL Document Store - The JSON/JSONB data type
Postgrtesql as a NoSQL Document Store - The JSON/JSONB data typeJumping Bean
 
Load testing in Zonky with Gatling
Load testing in Zonky with GatlingLoad testing in Zonky with Gatling
Load testing in Zonky with GatlingPetr Vlček
 
Java Concurrency and Asynchronous
Java Concurrency and AsynchronousJava Concurrency and Asynchronous
Java Concurrency and AsynchronousLifan Yang
 
An Intro to Scala for PHP Developers
An Intro to Scala for PHP DevelopersAn Intro to Scala for PHP Developers
An Intro to Scala for PHP DevelopersHuffPost Code
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScriptJorg Janke
 
Oslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep DiveOslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep Divedavanum
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring BootTrey Howard
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
Get the most out of Solr search with PHP
Get the most out of Solr search with PHPGet the most out of Solr search with PHP
Get the most out of Solr search with PHPPaul Borgermans
 

Similar to Action Jackson! Effective JSON processing in Spring Boot Applications (20)

Tour de Jackson: Forgotten Features of Jackson JSON processor
Tour de Jackson: Forgotten Features of Jackson JSON processorTour de Jackson: Forgotten Features of Jackson JSON processor
Tour de Jackson: Forgotten Features of Jackson JSON processor
 
Simple Jackson with DropWizard
Simple Jackson with DropWizardSimple Jackson with DropWizard
Simple Jackson with DropWizard
 
Spock pres
Spock presSpock pres
Spock pres
 
9.4json
9.4json9.4json
9.4json
 
Utopia Kindgoms scaling case: From 4 to 50K users
Utopia Kindgoms scaling case: From 4 to 50K usersUtopia Kindgoms scaling case: From 4 to 50K users
Utopia Kindgoms scaling case: From 4 to 50K users
 
Utopia Kingdoms scaling case. From 4 users to 50.000+
Utopia Kingdoms scaling case. From 4 users to 50.000+Utopia Kingdoms scaling case. From 4 users to 50.000+
Utopia Kingdoms scaling case. From 4 users to 50.000+
 
Node.js Course 1 of 2 - Introduction and first steps
Node.js Course 1 of 2 - Introduction and first stepsNode.js Course 1 of 2 - Introduction and first steps
Node.js Course 1 of 2 - Introduction and first steps
 
There is Javascript in my SQL
There is Javascript in my SQLThere is Javascript in my SQL
There is Javascript in my SQL
 
Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type
Postgrtesql as a NoSQL Document Store - The JSON/JSONB data typePostgrtesql as a NoSQL Document Store - The JSON/JSONB data type
Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type
 
Load testing in Zonky with Gatling
Load testing in Zonky with GatlingLoad testing in Zonky with Gatling
Load testing in Zonky with Gatling
 
Java Concurrency and Asynchronous
Java Concurrency and AsynchronousJava Concurrency and Asynchronous
Java Concurrency and Asynchronous
 
An Intro to Scala for PHP Developers
An Intro to Scala for PHP DevelopersAn Intro to Scala for PHP Developers
An Intro to Scala for PHP Developers
 
Dart the Better JavaScript
Dart the Better JavaScriptDart the Better JavaScript
Dart the Better JavaScript
 
Advanced PHP Simplified
Advanced PHP SimplifiedAdvanced PHP Simplified
Advanced PHP Simplified
 
Object Oriented Javascript
Object Oriented JavascriptObject Oriented Javascript
Object Oriented Javascript
 
Oslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep DiveOslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep Dive
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
Nightwatch 101 - Salvador Molina
Nightwatch 101 - Salvador MolinaNightwatch 101 - Salvador Molina
Nightwatch 101 - Salvador Molina
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Get the most out of Solr search with PHP
Get the most out of Solr search with PHPGet the most out of Solr search with PHP
Get the most out of Solr search with PHP
 

More from Joris Kuipers

Hearts Of Darkness - a Spring DevOps Apocalypse
Hearts Of Darkness - a Spring DevOps ApocalypseHearts Of Darkness - a Spring DevOps Apocalypse
Hearts Of Darkness - a Spring DevOps ApocalypseJoris Kuipers
 
I Can See Clearly Now - Observing & understanding your Spring applications at...
I Can See Clearly Now - Observing & understanding your Spring applications at...I Can See Clearly Now - Observing & understanding your Spring applications at...
I Can See Clearly Now - Observing & understanding your Spring applications at...Joris Kuipers
 
Day 2 Problems in CQRS & Event Sourcing
Day 2 Problems in CQRS & Event SourcingDay 2 Problems in CQRS & Event Sourcing
Day 2 Problems in CQRS & Event SourcingJoris Kuipers
 
Building and running Spring Cloud-based microservices on AWS ECS
Building and running Spring Cloud-based microservices on AWS ECSBuilding and running Spring Cloud-based microservices on AWS ECS
Building and running Spring Cloud-based microservices on AWS ECSJoris Kuipers
 
Booting your Microservices Architecture with Spring & Netflix
Booting your Microservices Architecture with Spring & NetflixBooting your Microservices Architecture with Spring & Netflix
Booting your Microservices Architecture with Spring & NetflixJoris Kuipers
 
Building Layers of Defense with Spring Security
Building Layers of Defense with Spring SecurityBuilding Layers of Defense with Spring Security
Building Layers of Defense with Spring SecurityJoris Kuipers
 
Come Fly With Me: Database Migration Patterns with Flyway
Come Fly With Me: Database Migration Patterns with FlywayCome Fly With Me: Database Migration Patterns with Flyway
Come Fly With Me: Database Migration Patterns with FlywayJoris Kuipers
 

More from Joris Kuipers (8)

Hearts Of Darkness - a Spring DevOps Apocalypse
Hearts Of Darkness - a Spring DevOps ApocalypseHearts Of Darkness - a Spring DevOps Apocalypse
Hearts Of Darkness - a Spring DevOps Apocalypse
 
I Can See Clearly Now - Observing & understanding your Spring applications at...
I Can See Clearly Now - Observing & understanding your Spring applications at...I Can See Clearly Now - Observing & understanding your Spring applications at...
I Can See Clearly Now - Observing & understanding your Spring applications at...
 
Day 2 Problems in CQRS & Event Sourcing
Day 2 Problems in CQRS & Event SourcingDay 2 Problems in CQRS & Event Sourcing
Day 2 Problems in CQRS & Event Sourcing
 
Boot Loot
Boot LootBoot Loot
Boot Loot
 
Building and running Spring Cloud-based microservices on AWS ECS
Building and running Spring Cloud-based microservices on AWS ECSBuilding and running Spring Cloud-based microservices on AWS ECS
Building and running Spring Cloud-based microservices on AWS ECS
 
Booting your Microservices Architecture with Spring & Netflix
Booting your Microservices Architecture with Spring & NetflixBooting your Microservices Architecture with Spring & Netflix
Booting your Microservices Architecture with Spring & Netflix
 
Building Layers of Defense with Spring Security
Building Layers of Defense with Spring SecurityBuilding Layers of Defense with Spring Security
Building Layers of Defense with Spring Security
 
Come Fly With Me: Database Migration Patterns with Flyway
Come Fly With Me: Database Migration Patterns with FlywayCome Fly With Me: Database Migration Patterns with Flyway
Come Fly With Me: Database Migration Patterns with Flyway
 

Recently uploaded

WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2WSO2
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...WSO2
 
WSO2CON 2024 - Software Engineering for Digital Businesses
WSO2CON 2024 - Software Engineering for Digital BusinessesWSO2CON 2024 - Software Engineering for Digital Businesses
WSO2CON 2024 - Software Engineering for Digital BusinessesWSO2
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & InnovationWSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & InnovationWSO2
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2
 

Recently uploaded (20)

WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
 
WSO2CON 2024 - Software Engineering for Digital Businesses
WSO2CON 2024 - Software Engineering for Digital BusinessesWSO2CON 2024 - Software Engineering for Digital Businesses
WSO2CON 2024 - Software Engineering for Digital Businesses
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & InnovationWSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 

Action Jackson! Effective JSON processing in Spring Boot Applications

  • 1. Action Jackson! Effective JSON processing in Spring Boot Applications
  • 5. Usage in Spring Boot ● spring-boot-starter-json ● REST controllers ● RestTemplate/WebClient ● Messaging ● State serialization in Batch, Session, … ● Various other places
  • 6. Ways of using Jackson ● Typically used for (un)marshalling ○ Directly or, more often, via framework ● To/from custom Java types ○ Often Java beans or records ● Various alternatives available
  • 8. Jackson Configuration ● Features ● Modules ● Annotations ● First two can be done by Boot ● Boot has its own defaults!
  • 9. Spring Boot’s Jackson Configuration ● Various modules registered ○ Service loader: Java Time ○ Boot: Parameters, Boot extensions ● Disables serializing dates/durations as numbers ● Unknown properties ignored ● Use properties or callbacks to tune
  • 10. Demo
  • 11. Customization Callback Example @Bean Jackson2ObjectMapperBuilderCustomizer offsetDateTimeSerializationCustomization() { return objectMapperBuilder -> objectMapperBuilder .serializerByType(OffsetDateTime.class, new JsonSerializer<OffsetDateTime>() { @Override public void serialize(OffsetDateTime value, JsonGenerator gen, SerializerProvider sp) throws IOException { gen.writeString(value.truncatedTo(SECONDS) .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); } }); }
  • 12. What Is A Field? ● Default is property-based access ● Note: getter defines field, setter is optional ● Can configure direct field access ○ With annotation or globally ● Constructor-based unmarshalling and records are supported ○ Some caveats; esp. for records, esp. for versions < 2.15
  • 13. Demo
  • 14. Parameter Names Module ● Prevents need for @JsonProperty(“propName”) ● Registered by default with Spring Boot ● Does require “-parameters” javac arg! ○ Add yourself when using Gradle ● By default single-arg constructor uses “delegating creator” (configurable, may want to disable this)
  • 15. (Un)marshalling alternatives Next to custom Java type, you can use: ● Map<String, Object> ○ Keys are fields, values are JSON values ○ String, number, List, Map for nested object ● Works both ways ● Use cases: ○ Ad-hoc HTTP request creation ○ Quick ‘n dirty response parsing ○ Wrapping custom type in top-level field
  • 16. Demo
  • 17. (Un)marshalling alternatives Next to custom Java type, you can use: ● JsonNode ○ Like a JSON DOM Tree ● Use as-is, or via JsonPointer or JSON Path ● Use cases: ○ Extracting specific detail(s) from large documents ○ Especially when deeply nested or querying on non-linear paths
  • 18. Demo
  • 19. Views ● Allow marshalling subset of fields ● Supported in Spring-MVC/Webflux ● Spring disables default view inclusion: only explicitly included fields are marshalled
  • 20. Demo
  • 21. Best Practices ● Create your ObjectMappers using Boot’s builder ● Re-use your ObjectMappers, also in tests! ● Be careful when using records for unmarshalling ● Write tests ● Stay away from abstract types ● Stop parsing JSON using regexps!
  • 22. Joris Kuipers CTO TRIFORK AMSTERDAM Thank you! trifork.nl @jkuipers, @triforkams
  • 23. Additional Annotation Options ● @JsonValue ○ Use annotated property as serialized value ● @JsonRawValue ○ Use property literally on serialization ● @JsonUnwrapped ○ Like a JPA Embeddable: flatten structure in JSON ○ Doesn’t support constructor-based unmarshalling ● @JsonFormat ○ Customize numbers, dates, collections or enums ○ Configure case-insensitive properties