I am checking for null value from the service which involves database transaction like this
if(!id.matches("-?\\d+(\\.\\d+)?") || contactService.getContact(id) == null){
throw new ResourceNotFoundException();
}
Contact contact = contactService.getContact(id);
But by this way I am calling getContact service twice and its a overhead for the application , any better way to check for null value, so that I just need to call my service once.
IllegalArgumentException
, and (2) you should precompile that pattern instead of using it inline and compiling on every call.