WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

RESTful Web Services · Basic · question 5 of 100

How do you handle errors in RESTful web services?

📕 Buy this interview preparation book: 100 RESTful Web Services questions & answers — PDF + EPUB for $5

Error handling is an important aspect of any web service, including RESTful web services. There are several ways to handle errors in RESTful web services:

HTTP Status Codes: RESTful web services use HTTP status codes to indicate the success or failure of an operation. HTTP status codes in the 2xx range indicate success, while codes in the 4xx range indicate client errors (such as bad requests) and codes in the 5xx range indicate server errors (such as internal server errors).

For example, a 404 status code indicates that the requested resource was not found on the server, while a 500 status code indicates that there was an internal server error.

Error Response Payloads: In addition to HTTP status codes, RESTful web services can also return error response payloads that provide more detailed information about the error. These payloads can be in JSON or XML format, and typically include an error message, error code, and any additional details about the error.

For example, here’s an error response payload in JSON format:

    {
        "error": {
            "code": "404",
            "message": "The requested resource was not found."
        }
    }

Exception Handling: In Java, exceptions can be used to handle errors in RESTful web services. Exceptions can be thrown by the application code or by the web service framework, and can be caught and handled by a global exception handler.

For example, if an exception occurs while processing a request in a RESTful web service, the web service framework might throw a javax.ws.rs.WebApplicationException exception. This exception can be caught and handled by a global exception handler that returns an appropriate error response to the client.

Here’s an example of how exception handling can be used in a RESTful web service implemented using the JAX-RS specification:

    import javax.ws.rs.*;
    import javax.ws.rs.core.*;
    
    @Path("/books")
    public class BookResource {
        
        @GET
        @Path("/{id}")
        @Produces(MediaType.APPLICATION\_JSON)
        public Book getBook(@PathParam("id") int id) {
            // retrieve the book with the given ID from the database
            Book book = BookDAO.getBookById(id);
            
            // throw a WebApplicationException if the book is not found
            if (book == null) {
                throw new WebApplicationException(Response.Status.NOT\_FOUND);
            }
            
            return book;
        }
        
        // global exception handler to handle WebApplicationException
        @Provider
        public class WebApplicationExceptionHandler implements ExceptionMapper<WebApplicationException> {
            public Response toResponse(WebApplicationException ex) {
                // create an error response payload
                ErrorResponse error = new ErrorResponse(ex.getResponse().getStatus(), ex.getMessage());
                return Response.status(ex.getResponse().getStatus()).entity(error).build();
            }
        }
    }

In this example, the getBook() method throws a WebApplicationException with a NOT_FOUND status code if the requested book is not found. The WebApplicationExceptionHandler class is a global exception handler that catches WebApplicationException and returns an appropriate error response to the client.

In summary, error handling is an important aspect of RESTful web services, and can be implemented using HTTP status codes, error response payloads, and exception handling.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic RESTful Web Services interview — then scores it.
📞 Practice RESTful Web Services — free 15 min
📕 Buy this interview preparation book: 100 RESTful Web Services questions & answers — PDF + EPUB for $5

All 100 RESTful Web Services questions · All topics