This project demonstrates various FasterXML Jackson annotations in a Spring Boot application. It showcases common JSON serialization and deserialization scenarios using Jackson annotations.
The project includes examples of the following Jackson annotations:
-
Basic JSON Serialization (
/hello)- Simple example of JSON response using Spring RestController
-
@JsonIgnore (
/json-ignore)- Demonstrates how to exclude fields from JSON serialization
- Example shows a field marked with
@JsonIgnorebeing excluded from the response
-
@JsonProperty (
/json-property)- Shows how to customize field names in JSON output
- Converts camelCase Java field names to snake_case JSON properties
-
@JsonInclude (
/json-include)- Demonstrates how to control which fields are included in JSON output
- Example shows excluding null fields from the response
-
@JsonFormat (
/json-format)- Shows how to format fields like dates during serialization
- Example demonstrates formatting dates with specific patterns and timezones
-
@JsonUnwrapped (
/json-unwrapped)- Demonstrates how to flatten nested objects during serialization
- Example shows unwrapping an
Addressobject into the main JSON structure
-
@JsonAnyGetter and @JsonAnySetter (
/json-any-getter-setter)- Shows how to handle arbitrary key-value pairs during serialization and deserialization
- Example demonstrates serializing a map into top-level properties and deserializing unknown properties into a map
-
@JsonTypeInfo and @JsonSubTypes (
/json-type)- Demonstrates handling polymorphic types during serialization and deserialization
- Example shows serializing and deserializing different
Devicesubtypes (Laptop,Desktop) based on atypefield
-
@JsonSerialize (
/json-serialize)- Shows how to use a custom serializer for a specific type
- Example demonstrates using a
GreetingSerializerto customize the JSON output
- Java 21
- Spring Boot 3.4.5
You can run the application using the Maven wrapper:
./mvnw spring-boot:runTo run the tests:
./mvnw testGET /hello- Returns a simple JSON messageGET /json-ignore- Demonstrates @JsonIgnore annotationGET /json-property- Shows @JsonProperty annotation usageGET /json-include- Demonstrates @JsonInclude annotationGET /json-format- Demonstrates @JsonFormat annotationGET /json-unwrapped- Demonstrates @JsonUnwrapped annotationPOST /json-any-getter-setter- Demonstrates @JsonAnyGetter and @JsonAnySetter annotationsPOST /json-type- Demonstrates @JsonTypeInfo and @JsonSubTypes annotationsPOST /json-serialize- Demonstrates @JsonSerialize with a custom serializer
- Spring Boot
- FasterXML Jackson
- JUnit 5
- MockMVC
- AssertJ