Home > Mobile >  How to return API response if id is not available in database
How to return API response if id is not available in database

Time:10-25

DetailsImpl.java

public detailResponse fetchId(String accId){
Optional<Acc> acc = accRep.findById(accId);
//Here I need to write the logic that if accId is not there in data base than I need to 
show the response from another API and the class service impl class for that is FeeDetails.java.

FeeDetails.java{

@Overeide
public List<Response> getRes(String accId){
return client.getDetailsOfField(accId);
}

I need to check if accId is there in database if it is present then logic is working fine but if it's not present than I need to show the response from already existing logic which is written in FeeDetails.java class. I am facing problem in writing the logic as it's giving an internal error when I hit the API.

CodePudding user response:

Optional.isPresent()

You can check using Optional.isPresent(), if the id is present in the database then return data from db else you can call the API to get the existing data.

public DetailResponse fetchId(String accId){

            Optional<Acc> acc = accRep.findById(accId);
            if(acc.isPresent()){
                //return from DB
            } else {
                //existing API code
            }
  }

CodePudding user response:

You can use a standard format

{"status":"success/failure","message":"your message"}

  • Related