Skip to content

Instantly share code, notes, and snippets.

@rponte
Last active March 11, 2019 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rponte/9ff07c981440583e48aa1021b2a6d634 to your computer and use it in GitHub Desktop.
Save rponte/9ff07c981440583e48aa1021b2a6d634 to your computer and use it in GitHub Desktop.
JSON responses with jSend Spec

JsonResult when SUCCESS:

  • Default Status : HTTP OK 200
  • Supported Status : HTTP Family 2xx
  • JSON Result Spec :
{ 
	status: "SUCCESS",  // Required
	data: Any,          // optional
	message: String,    // optional
	code: null          // always null
}
  • JSON Result samples:
// HTTP OK 200
{ 
	status: "SUCCESS",
	message: "Produto removido com sucesso!",
	code: null,
	data: null
}

// HTTP OK 200 - with payload
{ 
	status: "SUCCESS",
	message: "Produto gravado com sucesso!",
	code: null,
	data: {
		id: "2020",
		nome: "iPad Retina Display",
		preco: "2999.99",
		categorias: ["informatica", "apple", "..."],
		...
	}
}

JsonResult when FAIL:

  • Default Status : HTTP BAD REQUEST 400
  • Supported Status : HTTP Family 4xx
  • JSON Result Spec :
{ 
	status: "FAIL",      // Required
	data: Any,           // optional
	message: String,     // Required
	code: Integer        // optional
}
  • JSON Result samples:
// HTTP FORBIDDEN 403
{ 
	status: "FAIL",
	message: "Sem permissão para acessar funcionalidade",
	code: null,
	data: null
}

// HTTP BAD REQUEST 400 - with payload (Bean Validation)
{ 
	status: "FAIL",
	message: "Erro de validação",
	code: null,
	data: [
		{ field: "nome", message: "não pode ser vazio" },
		{ field: "preco", message: "valor não pode ser negativo" },
		...
	]
}

JsonResult when ERROR:

  • Default Status : HTTP INTERNAL SERVER ERROR 500
  • Supported Status : HTTP Family 5xx
  • JSON Result Spec :
{ 
	status: "ERROR",      // Required
	data: Any,            // optional
	message: String,      // Required
	code: Integer         // optional
}
  • JSON Result samples:
// HTTP INTERNAL SERVER ERROR 500
{ 
	status: "ERROR",
	message: "Produto já cadastrado no sistema!",
	code: null,
	data: null
}

// HTTP INTERNAL SERVER ERROR 500 - with payload
{ 
	status: "ERROR",
	message: "Produto substituido por nova versão",
	code: 101,
	data: {
		id: "9871",
		nome: "iPad Retina Display versão 2019",
		preco: "1800.00",
		categorias: ["apple", "..."],
		...
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment