JSON processing plays an important role in nearly all applications nowadays, and Jackson is the de-facto standard library for that. Most developers are therefore familiar with using it for simple (un)marshalling purposes, but never go beyond the basics. In this session Joris will show how to effectively configure and use Jackson, focusing on Spring Boot applications. Boot provides a lot of support for configuring ObjectMappers, which you can use directly but are also used internally by the framework for e.g. handling HTTP requests/responses or message conversion. Topics will include tuning mappings, using modules, applying views and using alternative parsing options like JSON Pointer and JsonPath, all illustrated through live demos. Don’t miss out on this presentation and become an Action Jackson yourself!
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
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
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
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
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
19. Views
● Allow marshalling subset of fields
● Supported in Spring-MVC/Webflux
● Spring disables default view inclusion:
only explicitly included fields are marshalled
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!
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