Home > Back-end >  How can I avoid null using lambda in this case?
How can I avoid null using lambda in this case?

Time:10-21

The following method you have seen returns all the information of the user in the database according to the phone number as follows.

{
  "message": "The operation was successful.",
  "code": 200,
  "fileDetailResponseDtos": [
    {
      "phoneNumber": "550000000",
      "textMessage": "Message1",
      "createDateTime": "2021-10-20T15:45:27.277",
      "sender": "Anar",
      "status": 0,
      "originatry": 0
    },
    {
      "phoneNumber": "550000000",
      "textMessage": "Message2",
      "createDateTime": "2021-10-20T15:45:27.277",
      "sender": "Anar",
      "status": 0,
      "originatry": 0
    },
    {
      "phoneNumber": "550000000",
      "textMessage": "Message3",
      "createDateTime": "2021-10-20T15:45:27.277",
      "sender": "Anar",
      "status": 0,
      "originatry": 0
    }
  ]
}

However, when I perform an operation based on a phone number that is not in the database, it still returns the second part instead of this message.

{
  "message": "The operation was successful",
  "code": 200,
  "fileDetailResponseDtos": []
}

But I want it to look like this in the output.

{
  "message": "Unfortunately, no information was found for this number.",
  "code": 400,
}

Yes I know it's not possible to return a list null. Here is the method I wrote in the dao layer.

How can I output the message I want when I perform an invalid operation?

@Override
public List<FileDetail> getByPhoneNumberCBVersion(String phoneNumber) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<FileDetail> generateQueryWithMethods = criteriaBuilder.createQuery(FileDetail.class);
    Root<FileDetail> starsFrom = generateQueryWithMethods.from(FileDetail.class);
    generateQueryWithMethods.select(starsFrom);
    generateQueryWithMethods.where(criteriaBuilder.equal(starsFrom.get("phoneNumber"), phoneNumber));
    TypedQuery<FileDetail> query = entityManager.createQuery(generateQueryWithMethods);
    List<FileDetail> resultList = query.getResultList();
    return resultList;
}

CodePudding user response:

If resultList is empty, then throw exception and catch it in ControllerAdvice and convert message to specific response.

IMHO : There is no need to throw exception when listing all data in the table. If it is empty you can list it to client(UI, Postman script ...) with 200 status code and list is empty.

Example of restcontrolleradvice when custom BadRequestException is thrown.

@RestControllerAdvice
public class ExceptionHandlerAdvice {
   @ExceptionHandler(value = BadRequestException.class)
    public ResponseEntity<ErrorResource> handleBadRequest(BadRequestException ex) {
        LOGGER.error(ex.getMessage(), ex);
        return ResponseEntity.status(ExceptionResponse.BAD_REQUEST.getStatus())
                .body(new ErrorResource(ExceptionResponse.BAD_REQUEST.getCode(),
                        String.format(ExceptionResponse.BAD_REQUEST.getMessage(), ex.getMessage())));
    }
}

If resultList is empty throw exception like below example.

if(CollectionUtils.isEmpty(resultList)) {
  throw new BadRequestException();
}
  • Related