mercredi 26 août 2020

Java - Avoid pattern duplication

Java In my server code, for each method I will have to do the following

    @Path("...")
    public Response function(...) {
        try {
            result = do some work to get result
            log the result
            return result
        } catch (Exception e) {
            log error
            return error
        }
    }

I will have to duplicate this pattern everywhere and only changing the do some work to get result part for new endpoint.

Is there a way that I can do to reduce this is duplication?

P.S. do some work to get result can be very something like getting a Json file, or accessing multiple database and calculate the result, and need to be in try-catch block.

Aucun commentaire:

Enregistrer un commentaire