Restful web services: Handling exception with WebApplicationException

Most of us use the below approach in our Rest services for handling the error response.

public Response getContactsById(@QueryParam("userId") String userId) {
        try {
            Contact contact = new ContactService().getContactsById(userId);
         return Response.ok().entity(contact).type(MediaType.APPLICATION_JSON).build();
        } catch (Exception ex) {
            LOGGER.error("Exception happened while getting contact data", ex);
            Throwable rootCause = Throwables.getRootCause(ex);
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).

So in the above code, if there is any issue/exception, then we catch that exception and get the root cause of it and create a new Response object with that and send it as JSON.

We can use the WebApplicationException instead of catching that error and creating a response object.

Below is the example for handling the above with WebApplicationException.

public class ContactNotFoundException extends WebApplicationException {

    public ContactNotFoundException(String message) {

public Response getContactsById(@QueryParam("userId") String userId) throws ContactNotFoundException {
        try {
            Contact contact = new ContactService().getContactsById(userId);
            return Response.ok().entity(contact).type(MediaType.APPLICATION_JSON).build();
        } catch (Exception ex) {
            LOGGER.error("Exception happened while getting contact data", ex);
            throw new ContactNotFoundException(ex.getMessage());

So in the above example, we create a custom exception ContactNotFoundException which extends the WebApplicationException. Inside the constructor, we create a response with proper status code and error message.

So in our Resource class, we use handle the exception and instead of creating a Response object, just throw the ContactNotFoundException with the message in it. Once we do this, then Rest of things are handled by Jersey framework itself.

Post Redirect Get Pattern

This is one of the web design pattern which is used to prevent the double form submission issue.

In the old days, when we design an application with JSP’s, we often face double form submission issue. So one of the best way to avoid this to disable the submit button as soon as the submit button is clicked. But if we show the result on same JSP page, then if the user refreshes the browser, then the form will be submitted again which cause some undesirable issues.

To avoid these kinds of errors/issues, we may need to change the API/server code to validate the request and some custom states. So it requires some unnecessary state maintenance in the server side.

Post Redirect Get pattern comes to the rescue in this kind of situation. Please refer the below diagram.



Here in this case, As soon as the user submits the form, the server does the operation on the backend and also forces the browser to redirect to a confirmation page. So if the user refreshes the confirmation page, then it would not have any impact as it will not do any operation other than showing the confirmation page.

But we have to keep in the below points along with this pattern.

  1. Disable the Submit button as soon as the user clicks on it
  2. If the server takes long time to respond back with the confirmation page and user tries to stop the browser and refresh the page in the mean time, then it will cause some other issues. So to handle these kind of issues, we have to either maintain some state and by checking this we can prevent the double submission issues.


Productivity : Stay Focused

In our day to life we keep on checking Facebook, Twitter and other entertainment sites.

We can’t control our mind by not checking on these sites. Due to this our productivity comes down drastically. If we get distracted by anything, then our mind will take lot of times to concentrate on that task.

There is a plugin called Stay Focused in Chrome which helps us to limit the amount of time spending on the social and entertainment sites. Once we install the plugin, we have to add the websites to be blocked in the settings and also configure the max allowable timing. Once the the max time reached, then you wont be able access those blocked sites in Stay focused. All those web sites are blocked by this plugin which help us to stay focus on our work.

Refer the below link,

Stay Focused

Productivity: Pomodoro Technique

The Pomodoro Technique is a time management method developed by Francesco Cirillo in the late 1980s

We can use this technique for managing our tasks. So there are six steps in this technique. They are given below

  1. Decide on the task to be done.
  2. Set the pomodoro timer (traditionally to 25 minutes).
  3. Work on the task until the timer rings. If a distraction pops into your head, write it down, but immediately get back on task.
  4. After the timer rings, put a checkmark on a piece of paper.
  5. If you have fewer than four checkmarks, take a short break (3–5 minutes), then go to step 1.
  6. Else (i.e. after four pomodoros) take a longer break (15–30 minutes), reset your checkmark count to zero, then go to step 1.

You can use the system timer or there are lots of Chrome plugin or apps available which you can use.

To name a few, Time doser, Pomodoro Timer, Simple Pomodoro and Strict Workflow

KanbanFlow app is integrated with Pomodoro timer. So we can easily manage our time with the KanbanFlow app.[]

Docker Custom container Name

We use Docker compose for multi container application. After starting all the containers, then if you run the docker ps, you will be able to see all the containers and its images and port details.

If you note down the container name, its something generated by Docker. If we want to use the custom name, then we should specify it as container_name in the Docker compose file. Refer the below example,

   image: rest_image:1.0
   container_name: mywebApp-rest 
      log_driver: "json-file"
     - 8081:8081
      max-size: "10m"
      max-file: "5"    

    image: web_image:1.0
    container_name: mywebApp-web
     - REST_URL=http://rest:8081
    log_driver: "json-file"
      - 80:80
       max-size: "10m"
       max-file: "5"
         - rest:rest

If you use docker-maven-plugin[], then you have to specify the alias inside the tag. Specify it as below


Copy File/Directory with Maven resource plugin

We can use Maven resource plugin to copy files and directory to any folder/path. Please refer the below snippet from pom.xml. This will be used to copy the config files from ${}/config to /opt/config folder.