Consider a Spring Boot REST controller method that returns a simple POJO. What JSON will be sent to the client?
public record User(String name, int age) {} @GetMapping("/user") public User getUser() { return new User("Alice", 30); }
Jackson uses field names as JSON keys by default and serializes numbers as numbers.
The record fields name and age become JSON keys with their exact names. The age is serialized as a number, not a string. The JSON is a flat object, not nested.
You want to configure Jackson in Spring Boot to skip null fields when serializing objects. Which code snippet achieves this?
Jackson's setSerializationInclusion controls which fields are included.
Using JsonInclude.Include.NON_NULL tells Jackson to skip fields with null values during serialization. Other options either disable unrelated features or include all fields.
Given this class and controller, why does the JSON output show {} instead of the expected fields?
public class Product { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public String getName() { return name; } public double getPrice() { return price; } } @GetMapping("/product") public Product getProduct() { return new Product("Book", 12.99); }
Jackson needs a way to read field values, usually via getters or public fields.
Jackson by default uses getters or public fields to serialize. Private fields without getters are ignored, resulting in empty JSON.
In Jackson, what does adding @JsonIgnoreProperties({"field1", "field2"}) to a class do?
Think about ignoring fields in both directions: reading and writing JSON.
@JsonIgnoreProperties tells Jackson to skip the listed fields when converting to or from JSON, so they are not included in output or expected in input.
Given this custom serializer registered for a class, what JSON will be produced?
public class Color { private int red, green, blue; public Color(int r, int g, int b) { red = r; green = g; blue = b; } public int getRed() { return red; } public int getGreen() { return green; } public int getBlue() { return blue; } } public class ColorSerializer extends StdSerializer<Color> { public ColorSerializer() { super(Color.class); } @Override public void serialize(Color value, JsonGenerator gen, SerializerProvider provider) throws IOException { String hex = String.format("#%02X%02X%02X", value.getRed(), value.getGreen(), value.getBlue()); gen.writeString(hex); } } // Registered in ObjectMapper @GetMapping("/color") public Color getColor() { return new Color(255, 165, 0); }
The custom serializer writes a string with the hex color code.
The serializer converts the Color object to a hex string like "#FFA500" and writes it as a JSON string, not an object.