Response Processor


Default Response Code

String success = "1200";
String partial = "1212";

String badRequest = "1400";
String unauthorized = "1401";
String forbidden = "1403";
String notFound = "1404";

String validationError = "1600";

String codeError = "1500";
String error = "1510";
String unknownError = "1511";
String otherError = "1512";

String hibernateError = "1580";


Default Exception Message

String invalidRequestParams = "Invalid Request Parameter";
String unknownError = "Unknown Error Occur";
String notFound = "Resource Not Found";
String codeError = "Internal Code Error";
String forbidden = "Resource Forbidden";
String badRequest = "Invalid Request";
String unauthorized = "You are not allowed";
String validationError = "Data Validation Error";


Regular Functionality

MessageResponse response(String message, String errorCode)

Create Message response with code

Example:

response("Success Response", "1313");

Output:

{
  "status": "success",
  "code": "1313",
  "message": {
    "text": "Success Response",
    "key": "success.response"
  }
}


MessageResponse response(String message)

Example:

response("Success Response");

Output:

{
  "status": "success",
  "code": "1200",
  "message": {
    "text": "Success Response",
    "key": "success.response"
  }
}


<E, D> PageableResponse<D> response(Page<E> page, Class<D> dto)

Example:

response(personRepository.list(entityList, PersonMasterDTO.class)

Output:

{
  "status": "success",
  "code": "1200",
  "data": [
    {
      "id": 5,
      "firstName": "Person",
      "lastName": "Four",
      "mobile": "123456",
      "gender": "Female",
      "age": 50
    }
  ],
  "pagination": {
    "total": 2,
    "totalPage": 1,
    "page": 0,
    "itemPerPage": 15
  }
}


<E, D> DetailsResponse<D> response(E source, Class<D> dto, String message)

Example:

response(personRepository.findById(id), PersonDetailDTO.class, "Item not found")

Output:

{
  "status": "success",
  "code": "1200",
  "data": {
    "id": 1,
    "firstName": "Touhid",
    "lastName": "Mia",
    "mobile": "123456",
    "gender": "Male",
    "age": 28,
    "address": "Dhaka, Bangladesh",
    "email": "problemfighter.com@gmail.com",
    "occupation": "Software Engineer"
  }
}

{
  "status": "error",
  "code": "1510",
  "error": {
    "message": {
      "text": "Item not found",
      "key": "item.not.found"
    }
  }
}


<E, D> DetailsResponse<D> response(E source, Class<D> dto)

Example:

response(personRepository.findById(id), PersonDetailDTO.class)

Output:

{
  "status": "success",
  "code": "1200",
  "data": {
    "id": 1,
    "firstName": "Touhid",
    "lastName": "Mia",
    "mobile": "123456",
    "gender": "Male",
    "age": 28,
    "address": "Dhaka, Bangladesh",
    "email": "problemfighter.com@gmail.com",
    "occupation": "Software Engineer"
  }
}


<D> BulkResponse<D> response(BulkErrorValidEntities<D, ?> processed, Class<D> dto)

Example:

BulkErrorValidEntities<PersonUpdateDTO, Person> bulkData = dataUtil().merge(entities, data);
response(bulkData, PersonUpdateDTO.class);

Output:

{
  "status": "partial",
  "code": "1212",
  "success": [
    {
      "id": 5,
      "firstName": "Person",
      "lastName": "Four",
      "mobile": "123456",
      "gender": "Female",
      "age": 50,
      "address": "NYC, USA",
      "email": "email13@email.local",
      "occupation": "Software Designer"
    }
  ],
  "failed": [
    {
      "error": {
        "message": {
          "text": "Unable to process update",
          "key": "unable.to.process.update"
        }
      },
      "object": {
        "id": 100,
        "firstName": "Touhid",
        "lastName": "Mia",
        "mobile": "123456",
        "gender": "Male",
        "age": 24,
        "address": "Dhaka, Bangladesh",
        "email": "problemfighter.com@gmail.com",
        "occupation": "Senior Software Engineer"
      }
    }
  ]
}


<E, D> D entityToDTO(E entity, Class<D> dto)

Example:

PersonUpdateDTO dto = entityToDTO(entityObject, PersonUpdateDTO.class)


MessageResponse error(String message, String errorCode)

Example:

error("Error Response", "3131");

Output:

{
  "status": "error",
  "code": "3131",
  "error": {
    "message": {
      "text": "Error Response",
      "key": "error.response"
    }
  }
}


MessageResponse error(String message)

Example:

error("Error Response");

Output:

{
  "status": "error",
  "code": "1510",
  "error": {
    "message": {
      "text": "Error Response",
      "key": "error.response"
    }
  }
}


Static Method for Remote usage

static ResponseProcessor instance()

Static method for create Instance.


static MessageResponse unknownError()

Example:

ResponseProcessor.unknownError()

Output:


static MessageResponse notFound()

Example:

ResponseProcessor.notFound()

Output:

{
  "status": "error",
  "code": "1404",
  "error": {
    "message": {
      "text": "Resource Not Found",
      "key": "resource.not.found"
    }
  }
}


static MessageResponse badRequest()

Example:

ResponseProcessor.badRequest()

Output:

{
  "status": "error",
  "code": "1400",
  "error": {
    "message": {
      "text": "Invalid Request",
      "key": "invalid.request"
    }
  }
}


static MessageResponse unauthorized()

Example:

ResponseProcessor.unauthorized()

Output:

{
  "status": "error",
  "code": "1401",
  "error": {
    "message": {
      "text": "You are not allowed",
      "key": "you.are.not.allowed"
    }
  }
}


static MessageResponse forbidden()

Example:

ResponseProcessor.forbidden()

Output:

{
  "status": "error",
  "code": "1403",
  "error": {
    "message": {
      "text": "Resource Forbidden",
      "key": "resource.forbidden"
    }
  }
}


static MessageResponse codeError()

Example:

ResponseProcessor.codeError()

Output:

{
  "status": "error",
  "code": "1500",
  "error": {
    "message": {
      "text": "Internal Code Error",
      "key": "internal.code.error"
    }
  }
}


static MessageResponse validationError()

Example:

ResponseProcessor.validationError()

Output:

{
  "status": "error",
  "code": "1600",
  "error": {
    "message": {
      "text": "Data Validation Error",
      "key": "data.validation.error"
    }
  }
}


static MessageResponse errorMessage(String message)

Example:

ResponseProcessor.errorMessage("Error Message")

Output:

{
  "status": "error",
  "code": "1510",
  "error": {
    "message": {
      "text": "Error Message",
      "key": "error.message"
    }
  }
}


static MessageResponse successMessage(String message)

Example:

ResponseProcessor.successMessage("Success Message")

Output:

{
  "status": "success",
  "code": "1200",
  "message": {
    "text": "Success Message",
    "key": "success.message"
  }
}


static MessageResponse otherError(String message)

Example:

ResponseProcessor.otherError("Other Message")

Output:

{
  "status": "error",
  "code": "1512",
  "error": {
    "message": {
      "text": "Other Message",
      "key": "other.message"
    }
  }
}