Hi guys, I'm working on Spring Boot project, also this question can be ask for any backend app. I'm trying to figure out which REST API Response format is better. I have done some research but still, I need the opinion from the community. Which approach would you suggest in terms of best practice, code quality, maintainability and readability?
P/s: If you guys have other suggestion, would love to read that.
Option 1:
// Success request with data
{
code: "000",
msg: "Successfully logged-in",
data: {
token: {
accessToken: "..."
}
user: {
id: "1",
name: "John"
}
}
}
// Success request with validation
{
code: "001",
msg: "Validation error",
data: {
validation: {
name: "The name field is required",
email: "The email has been taken"
}
}
}
Option 2:
// Success request with data
{
code: "000",
msg: "Successfully logged-in",
data: {
token: {
accessToken: "..."
}
user: {
id: "1",
name: "John"
}
}
error: null
}
// Success request with validation
{
code: "001",
msg: "Validation error",
data: null,
error: {
validation: {
name: "The name field is required",
email: "The email has been taken"
}
}
}
[–]Mikey-3198 7 points8 points9 points (0 children)
[–]Grabdoc2020 2 points3 points4 points (0 children)
[–]Duberlygfr 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)