REST resources can have multiple representations (e.g., JSON, XML) as different clients may request different formats. The mechanism for selecting the appropriate representation is called content negotiation. By default, it uses application/json
unless modified.
Content negotiation enables a single endpoint to support various resource representation types based on client requests.
Content negotiation can be implemented in the following ways, listed by precedence:
.json
, .xml
, or .txt
./resource.json
or /resource.xml
.format=xml
or format=json
./resource?format=json
.Accept
header.Accept: application/json
or Accept: application/xml
.Spring Boot uses Jackson as the default vendor for marshalling (serialization: Java → JSON) and unmarshalling (deserialization: JSON → Java).
To customize the names of properties during serialization and deserialization, use the Jackson annotation @JsonProperty
.
Example:
@JsonProperty(value="mesg")
private String message;
This maps the Java field message
to the JSON property mesg
.
You can control property access during serialization and deserialization using the access
attribute of @JsonProperty
:
@JsonProperty(access = Access.WRITE_ONLY)